mimre
mimre

Reputation: 92

Make/Cmake subdirectory linking to external library fails

I'm currently having trouble in the following setup:

My main project has a subdirector that is a library. This library depends on a system library "triangle" (installed from source). The main project does use a file from the subdirectory. Cmake of the main project (and the library) work fine.

Building the library works just fine. (Either in it's own directory or after cmake in the main directory with make subdir_lib compiles without problems)

This is where the problems starts. Building the main project with make project fails. It happens during linking:

subdir/libsubdir_lib.a(Test.cpp.o): In function `Test::run()':
/home/mimre/workspace/tmp/cmake-problem/subdir/files/Test.cpp:34: undefined reference to `triangle_context_create'
/home/mimre/workspace/tmp/cmake-problem/subdir/files/Test.cpp:35: undefined reference to `triangle_context_options'
/home/mimre/workspace/tmp/cmake-problem/subdir/files/Test.cpp:42: undefined reference to `triangle_mesh_create'
/home/mimre/workspace/tmp/cmake-problem/subdir/files/Test.cpp:50: undefined reference to `triangle_context_destroy'
collect2: error: ld returned 1 exit status
CMakeFiles/cmake_problem.dir/build.make:95: recipe for target 'cmake_problem' failed
make[3]: *** [cmake_problem] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/cmake_problem.dir/all' failed
make[2]: *** [CMakeFiles/cmake_problem.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/cmake_problem.dir/rule' failed
make[1]: *** [CMakeFiles/cmake_problem.dir/rule] Error 2
Makefile:118: recipe for target 'cmake_problem' failed
make: *** [cmake_problem] Error 2

To avoid having a wall of code in here, I uploaded a minimal example onto github: https://github.com/mimre25/cmake_problem

Also, this is the library I'm using, installed with cmake & sudo make install: https://github.com/wo80/Triangle

I've tried the solutions from various similar threads but to no avail.

Thanks in advance for any help!

Upvotes: 0

Views: 1509

Answers (2)

Hashimoto
Hashimoto

Reputation: 346

I would have written this as a comment but I don't have enough reputation for that. Is this a situation where you need to use this Triangle (https://github.com/wo80/Triangle), rather than the original Triangle (https://www.cs.cmu.edu/~quake/triangle.html)? If you can use the latter, I know from experience that its is very easy to link to. I just put it in a subdirectory in my code with this CMakeLists.txt.

## This only works for linux. Use an if statement to handle all architectures.
SET(CMAKE_C_FLAGS
  "${CMAKE_C_FLAGS} -O -DLINUX -DTRILIBRARY -w -DANSI_DECLARATORS"
  )

SET(FILES_SOURCE
  triangle.h triangle.c
  )

ADD_LIBRARY( my_local_name_for_triangle_library STATIC ${FILES_SOURCE} )

And then I can link to the triangle library I have created like this:

include_directories(my_local_triangle_dir)
target_link_libraries(my_local_name_for_triangle_library)

However, some of the #define macros are missing in triangle.h, so you need to copy them from triangle.c to triangle.h.

Upvotes: 1

Ben
Ben

Reputation: 298

It seems that you tried to link a library that doesn't exist (where CMake can find it).

You either need to create a find_library, or when you link to triangle, give a full path with name.

Alternatively, you can leave the source in a sub directory which you can call then link to the name.

Upvotes: 0

Related Questions