Question

I have a problem with the usage of CMake on Linux 64 bit. I have a sample in C that must link to a library (xr_arp.a) which has a link dependency on another lib (libcrypto.a). The following makefile that I did to build my sample code is linking successfully:

CFLAGS = -I../Common -I../xr -I../../../openssl/include
LIBS = ../xr/xr_arp.a ../../../openssl/lib/libcrypto.a
TARGET = sample
OBJFILES = sample.o 

all: $(TARGET)

$(TARGET): Makefile $(OBJFILES)
    $(CC) -o $@ $(OBJFILES) $(LIBS)

clean:
    rm -rf *.o $(TARGET)

.SUFFIXES: .c .o

.c.o:
    $(CC) -Wall $(CFLAGS) -c $<

However, I would like to convert this makefile to use CMake instead. When I use the following CMakeLists.txt file, I get that xr_arp.c has undefined reference to `SHA1' as it seems like it cannot link xr_arp.a to libcrypto.a:

cmake_minimum_required(VERSION 2.8)
project (SAMPLE C)

set(CMAKE_C_FLAGS "-Wall")

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/../Common
    ${CMAKE_CURRENT_SOURCE_DIR}/../xr
)

add_executable(
    sample
    sample.c
)

target_link_libraries(
    sample
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../openssl/lib/libcrypto.a
    ${CMAKE_CURRENT_SOURCE_DIR}/../xr/xr_arp.a
)

Can someone point me out on what is the difference between those two files? Why is it working with the makefile and it isn't with CMake? Is there any procedure I could use to force the linking between xr_arp.a and libcrypto.a? Please take note that both libs are third-party and are not mine.

Was it helpful?

Solution

Have you tried switching the order the libraries are specified in TARGET_LINK_LIBRARIES? In many cases it shouldn't make a difference, but I've run into cases with certain linkers where it does. I believe the GNU linker on Linux is one case where it makes a difference.

I think that xr_arp.a should appear first, so that the linker knows to look for SHA1 in the libraries that follow it.

Failing that, try running "make VERBOSE=1" and comparing the linker command that is generated by CMake with the one generated by the Makefile.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top