【发布时间】:2017-07-21 11:12:38
【问题描述】:
我正在学习 Cmake 并在简单的 C 项目中添加库项目。我创建了一个具有以下结构的简单库项目。
MyLibraryProjectDirectory
|
-- library.C
-- library.h
-- UmerDir
|
-- Umer.h
-- Umer.c
Cmakelist.txt 正在生成一个.a lib 文件,我正在另一个 C 程序中使用该文件。
C 程序的结构如下:
MyCProgram
|
-- main.c
-- libuntitled.a
libuntitled.a是我的库项目中CmakeList.txt生成的库文件。
问题是当我尝试使用 .a 文件构建 C 项目时,它会抛出以下内容
错误:
"C:\Program Files\JetBrains\CLion 2017.2\bin\cmake\bin\cmake.exe" --build C:\Users\Lenovo\CLionProjects\AppProject\cmake-build-debug --target AppProject -- -j 2
[ 50%] Linking C executable AppProject.exe
C:/mingw-w64/i686-7.1.0-posix-dwarf-rt_v5-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.1.0/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -llibuntitled
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [AppProject.exe] Error 1
CMakeFiles\AppProject.dir\build.make:95: recipe for target 'AppProject.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/AppProject.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/AppProject.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/AppProject.dir/rule] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/AppProject.dir/rule' failed
mingw32-make.exe: *** [AppProject] Error 2
Makefile:117: recipe for target 'AppProject' failed
我不确定我做错了什么,所以请原谅我在 CMake 和 C 构建系统方面的知识极其有限。
为了方便,我还附上了C程序和C库项目的截图。
图书馆项目截图:
C 程序项目:
编辑:
我已通过在 .a 库名称前添加 ${CMAKE_SOURCE_DIR} 来修复未找到库的问题。显然,即使在同一目录中,Cmake 也需要完整的分类路径来定位 .a 文件,这有点奇怪。无论如何,现在我收到以下错误:
错误:
"C:\Program Files\JetBrains\CLion 2017.2\bin\cmake\bin\cmake.exe" --build C:\Users\Lenovo\CLionProjects\AppProject\cmake-build-debug --target AppProject -- -j 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/AppProject.dir/all' failed
mingw32-make.exe[3]: *** No rule to make target '../libuntitled', needed by 'AppProject.exe'. Stop.
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/AppProject.dir/rule' failed
mingw32-make.exe[2]: *** [CMakeFiles/AppProject.dir/all] Error 2
Makefile:117: recipe for target 'AppProject' failed
mingw32-make.exe[1]: *** [CMakeFiles/AppProject.dir/rule] Error 2
mingw32-make.exe: *** [AppProject] Error 2
mingw32-make.exe3: 没有规则来制作目标 '../libuntitled','AppProject.exe' 需要。停止。
【问题讨论】:
-
target_link_libraries(AppProject untitled)不是“libuntitled”。 -
问题已解决。请看下面的答案。我还不能接受我自己的答案作为已接受的答案
标签: c++ c cmake static-libraries clion