【问题标题】:Unable to link .a library file in C Program无法在 C 程序中链接 .a 库文件
【发布时间】: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' 需要。停止。

【问题讨论】:

标签: c++ c cmake static-libraries clion


【解决方案1】:

除此之外,我不会在第一个 CMake-Project 中创建库并在第二个中手动链接它。这可能会对您有所帮助:How do I tell CMake to link in a static library in the source directory?

但是您应该(如果可能)寻找一种方法来创建带有子项目的单个 CMake-Project。这里有一个很好的解释的链接:http://techminded.net/blog/modular-c-projects-with-cmake.html

【讨论】:

  • 我明白了。我这样做的原因是因为我有另一个巨大的 C 项目,我将使用它来制作一个 .a 文件,然后在另一个 C 程序中使用该 .a 文件
  • 我理解你的问题。独立项目的问题是如果你的 huge 库有任何改变,你必须手动编译这个库,最好在你的c 可执行程序。在单个项目中,CMake 会检查所有依赖项,如果进行了更改,则重新编译它们。
【解决方案2】:

我不熟悉cmake,但它应该生成-luntitled,而不是-llibuntitled,以链接libuntitled.a

【讨论】:

    【解决方案3】:

    我已通过在 .a 库名称前添加 ${CMAKE_SOURCE_DIR} 来修复未找到库的问题。显然,即使在同一目录中,Cmake 也需要完整的分类路径来定位 .a 文件,这有点奇怪。

    CmakeList.txt 现在看起来像这样

    target_link_libraries(AppProject ${CMAKE_SOURCE_DIR}/${MY_LIB})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 2020-05-15
      • 2017-09-23
      • 1970-01-01
      相关资源
      最近更新 更多