MAKE/CMAKE子目录链接到外部库的链接失败

Make/Cmake subdirectory linking to external library fails

本文关键字:链接 失败 外部 CMAKE 子目录 MAKE      更新时间:2023-10-16

我当前在以下设置中遇到麻烦:

我的主要项目有一个库的子导演。该库取决于系统库"三角形"(从源安装(。主项目确实使用子目录中的文件。主项目(和图书馆(的cmake工作正常。

构建图书馆的工作正常。(无论是在其自己的目录中还是在CMAKE之后在主目录中使subdir_lib毫无问题地编译(

这是问题开始的地方。用"制造项目"失败建立主要项目。它发生在链接期间:

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&sudo make install:https://github.com/wo80/triangle

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

事先感谢您的任何帮助!

我会写这句话,但我没有足够的声誉。是您需要使用此三角形(https://github.com/wo80/triangle(而不是原始三角(https://www.c.cmu.edu/~quake/triangle.html(而不是这种情况(https://wo80/triangle(?如果您可以使用后者,我从经验中知道,它很容易链接到。我只是用这个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)

但是,三角形中缺少#Define宏中的一些,因此您需要将它们从Triangle.C复制到Triangle.H。

似乎您尝试链接不存在的库(cmake可以找到它(。

您要么需要创建一个find_library,要么在链接到三角形时,用名称给出完整的路径。

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