0

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!

2
  • What does your Makefile look like? Commented Feb 7, 2018 at 17:08
  • The makefile is generated by cmake. Commented Feb 7, 2018 at 17:12

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately I run into the same problem. It works all fine when building subdir_lib, but once I'm in the upper most directory, I get undefined reference to 'triangulate(char*, triangulateio*, triangulateio*, triangulateio*)' collect2: error: ld returned 1 exit status.. triangle is in a subdirector of subdir (I've update the github repository)
Oh you should include the library like this: extern "C"{ #include <triangle.h> } if you are using it in C++.
0

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.

15 Comments

But why does it work when building the sub directory? It also links the library
In the sub directory you have add_library(subdir_lib. That allows the library to be added by the name subdir_lib.
Yes, I have that: In the subdirectory: add_library(subdir_lib files/Test.cpp files/Test.h) target_link_libraries(subdir_lib triangle), in the main directory add_executable(cmake_problem main.cpp) target_link_libraries(cmake_problem subdir_lib triangle)
Define triangle. You know it as the built file, but CMake doesn't know what it is. If you add the downloaded source as a sub directory, there is an add_library call naming the library "triangle". Short of that, you have to tell CMake where the library is. Either you have to find it, or explicitly say where the built library exists.
Even if I add find_package(Triangle REQUIRED) to both the CMakeLists.txt files it doesn't work. But Cmake does not stop the preocessing, which means it finds the packages..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.