Make / Cmake子目录链接到外部库失败

时间:2018-02-07 17:03:01

标签: c++ c makefile cmake linker

我目前在以下设置中遇到问题:

我的主项目有一个子目录,它是一个库。这个库取决于系统库"三角形" (从源头安装)。主项目确实使用子目录中的文件。 主项目(和图书馆)的Cmake工作正常。

构建库工作得很好。 (在其自己的目录中或在主目录中的cmake之后) 使subdir_lib编译没有问题)

这是问题的开始。 使用make项目构建主项目失败。它发生在链接期间:

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

为了避免在这里插入代码,我在github上发了一个最小的例子:https://github.com/mimre25/cmake_problem

此外,这是我使用的库,安装了cmake& amp; sudo make install:https://github.com/wo80/Triangle

我已尝试过各种类似线程的解决方案,但无济于事。

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我会把它写成评论,但我没有足够的声誉。这是您需要使用此三角形(https://github.com/wo80/Triangle)而不是原始三角形(https://www.cs.cmu.edu/~quake/triangle.html)的情况吗?如果你可以使用后者,我从经验中知道它很容易链接到。我只是把它放在我的代码中的一个子目录中,带有这个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} )

然后我可以链接到我创建的三角形库:

include_directories(my_local_triangle_dir)
target_link_libraries(my_local_name_for_triangle_library)

但是,triangle.h中缺少一些#define宏,所以你需要将它们从triangle.c复制到triangle.h。

答案 1 :(得分:0)

您似乎试图链接一个不存在的库(CMake可以找到它)。

你需要创建一个find_library,或者当你链接到三角形时,给出一个带名字的完整路径。

或者,您可以将源保留在可以调用的子目录中,然后链接到名称。

相关问题