Question

I'm using a modified replacement for backtrace_symbols in a project ( found here: http://cairo.sourcearchive.com/documentation/1.9.4/backtrace-symbols_8c-source.html )

This replacement depends on libbfd, libiberty, and libdl. Is there any way I can detect of these three libraries have been linked, and only include the header for this if that is the case? Otherwise I just want to fall back on built-in backtrace_symbols from execinfo.h.

If this seems undoable on its own, is it possible to detect at compile time if these libraries are available, and if they are combine them into the object file I'm using them in? Otherwise I can then pass in a define macro with -D at compile to to know if they were available or not.

Here is the makefile for the object file that uses the modified backtrace_symbols:

CC = clang
CFLAGS = -g -Wall -c
SOURCE = simplog.c
OBJ = simplog.o simplog-temp.o backtrace-symbols.o

all:
    $(CC) $(CFLAGS) $(SOURCE); mv simplog.o simplog-temp.o; \
    $(CC) -ansi $(CFLAGS) backtrace-symbols.c; \
    ld -r simplog-temp.o backtrace-symbols.o -o simplog.o

clean:
    rm -f $(OBJ) 

As you can see I am combining backtrace-symbols.c with my logger into the same object file. Is there a way I could detect here if libbfd, liberty, and libdl are available, and if they are combine them into this object file as well? Currently I am linking them into the final executable that links in this logger object file, but I would much prefer to do it at this stage if possible.

Was it helpful?

Solution

So it looks like it is actually impossible to link static libraries into a static library as I was trying to do, but it is possible to conditionally include a library/libraries if they do not exist. The following CMakeLists code shows a basic way of checking if some required libraries exist and if they do including them in the final library, and giving a #define in the code to know if they are available to you or not:

cmake_minimum_required(VERSION 2.8)
project(simplog)

configure_file (
    "SimpLogConfig.h.in"
    "SimpLogConfig.h"
    @ONLY
)

set( CMAKE_C_COMPILER "clang" )

find_library( BFD_LIBRARY bfd )
find_library( IBERTY_LIBRARY iberty )

find_path(
    IBERTY_HEADER_PATH libiberty.h
    PATHS
        /usr/include/libiberty
        /usr/local/include/libiberty.h
)

include_directories(
    ${IBERTY_HEADER_PATH}
    ${PROJECT_BINARY_DIR}
)

set( PACKAGE "SimpLog" )
set( PACKAGE_VERSION "0.0.1" )
if( BFD_LIBRARY AND IBERTY_LIBRARY )
    option( BETTER_BACKTRACE "" ON )
    add_library( backtrace-symbols STATIC backtrace-symbols.c )
    target_link_libraries( backtrace-symbols ${BFD_LIBRARY} ${IBERTY_LIBRARY} ${CMAKE_DL_LIBS} )
    set( LIBS $(LIBS) backtrace-symbols )
else()
    option( BETTER_BACKTRACE "" OFF )
    set( LIBS $(LIBS) "" )
endif()

add_library( simplog STATIC simplog.c )
target_link_libraries( simplog $(LIBS) )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top