How to use shared lib for static with cmake

563 Views Asked by At

I'm beginner with CMake and i would to compile my project with it. I have the following CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)
project (rt)

include_directories ("${PROJECT_BINARY_DIR}/src/")
include_directories ("${PROJECT_BINARY_DIR}/src/hook")
include_directories ("${PROJECT_BINARY_DIR}/src/light")
include_directories ("${PROJECT_BINARY_DIR}/src/move")
include_directories ("${PROJECT_BINARY_DIR}/src/object/inter")
include_directories ("${PROJECT_BINARY_DIR}/src/object/normal")
include_directories ("${PROJECT_BINARY_DIR}/src/option")
include_directories ("${PROJECT_BINARY_DIR}/src/parsing")
include_directories ("${PROJECT_BINARY_DIR}/src/ray")
include_directories ("${PROJECT_BINARY_DIR}/src/render")
include_directories ("${PROJECT_BINARY_DIR}/src/shadow")
include_directories ("${PROJECT_BINARY_DIR}/src/utils")
include_directories ("${PROJECT_BINARY_DIR}/lib/minilibx")

file(GLOB hook
    "*.h"
    "*.c"
)

file(GLOB_RECURSE SRC_FILES ${PROJECT_SOURCE_DIR}/src/*.c)

#set (EXTRA_LIBS ${EXTRA_LIBS} ${X11_LIBRARIES} m)

add_executable (rt ${SRC_FILES})
target_link_libraries (rt ${EXTRA_LIBS} X11 Xext m ${PROJECT_BINARY_DIR}/lib/libmlx.a)

But when I run make, my static library minilibx won't compile. with errors that all the functions from the X11 and Xext libraries are undefined.

make VERBOSE=1 result:

Linking C executable rt /usr/bin/cmake -E cmake_link_script CMakeFiles/rt.dir/link.txt --verbose=1 /usr/bin/cc CMakeFiles/rt.dir/src/move/translation.c.o CMakeFiles/rt.dir/src/move/rotation.c.o CMakeFiles/rt.dir/src/light/light.c.o CMakeFiles/rt.dir/src/option/option.c.o CMakeFiles/rt.dir/src/option/file_option.c.o CMakeFiles/rt.dir/src/render/render.c.o CMakeFiles/rt.dir/src/hook/control.c.o CMakeFiles/rt.dir/src/ray/eye.c.o CMakeFiles/rt.dir/src/ray/ray.c.o CMakeFiles/rt.dir/src/utils/my_strlen.c.o CMakeFiles/rt.dir/src/utils/my_strncpy.c.o CMakeFiles/rt.dir/src/object/inter/plan.c.o CMakeFiles/rt.dir/src/object/inter/cone.c.o CMakeFiles/rt.dir/src/object/inter/cylinder.c.o CMakeFiles/rt.dir/src/object/inter/sphere.c.o CMakeFiles/rt.dir/src/object/inter/tore.c.o CMakeFiles/rt.dir/src/object/normal/plan.c.o CMakeFiles/rt.dir/src/object/normal/cone.c.o CMakeFiles/rt.dir/src/object/normal/cylinder.c.o CMakeFiles/rt.dir/src/object/normal/sphere.c.o CMakeFiles/rt.dir/src/parsing/scene.c.o CMakeFiles/rt.dir/src/parsing/file_access.c.o CMakeFiles/rt.dir/src/parsing/str_tool.c.o CMakeFiles/rt.dir/src/shadow/shadow.c.o CMakeFiles/rt.dir/src/main.c.o -o rt -rdynamic -lX11 -lXext -lm lib/libmlx.a

and this is an example of errors:

lib/libmlx.a(mlx_init.o): In function mlx_int_deal_shm': mlx_init.c:(.text+0x2a): undefined reference toXShmQueryVersion' mlx_init.c:(.text+0x104): undefined reference to `XShmPixmapFormat'

How do I solve the undefined reference errors?

2

There are 2 best solutions below

0
On BEST ANSWER

I have solved the problem, the order of libs are not correct.

I just link my lib before another lib. Et voilà :)

target_link_libraries (rt ${EXTRA_LIBS} ${PROJECT_BINARY_DIR}/lib/libmlx.a X11 Xext m)

0
On

CMake uses target_link_libraries to specify which libraries you link against.
In you case, it seems like you're linking against the correct libraries (target_link_libraries (rt ${EXTRA_LIBS} X11 Xext m ${PROJECT_BINARY_DIR}/lib/libmlx.a)), so my bet is they are not found.

Try running:
make VERBOSE=1

This will give you the gcc output. Paste the last couple of gcc instructions it generates here and we'll see why it didn't find your libs.