首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >包含带有cmake的静态库

包含带有cmake的静态库
EN

Stack Overflow用户
提问于 2018-05-19 23:07:20
回答 1查看 5.4K关注 0票数 4

我正在学习C++。最让人困惑的事情之一是学习如何在使用CMake时链接库(我在Windows上使用CLion,它使用CMake)。我从这里下载了GLFW的预构建二进制文件。我抓取了includes文件夹和使用mingw构建的库文件夹。我将必要的文件放在项目中的依赖项文件夹中。我的文件夹结构是这样的。

文件夹结构

代码语言:javascript
复制
Project
├── bin
├── build
└── src
    ├── CMakeLists.txt
    ├── dependencies
    │   └── GLFW
    │       ├── include
    │       │   └── GLFW
    │       │       ├── glfw3.h
    │       │       └── glfw3native.h
    │       └── lib
    │           └── libglfw3.a
    ├── lib
    └── main.cpp

代码:(从GLFW文档这里中提取的初始代码)

代码语言:javascript
复制
#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

CmakeList.txt

代码语言:javascript
复制
cmake_minimum_required(VERSION 3.10)
project(cmake)

set(CMAKE_CXX_STANDARD 11)
set(EXECUTABLE_OUTPUT_PATH ../bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

target_include_directories(dependencies/GLFW/include)

add_executable(cmake main.cpp)

target_link_libraries(cmake dependencies/GLFW/lib/libglfw3.a)

误差

代码语言:javascript
复制
C:\Users\username\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\181.4668.70\bin\cmake\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\username\CLionProjects\cmake\src
CMake Error at CMakeLists.txt:11 (target_include_directories):
  target_include_directories called with incorrect number of arguments


-- Configuring incomplete, errors occurred!
See also "C:/Users/username/CLionProjects/cmake/build/CMakeFiles/CMakeOutput.log".

[Failed to reload]

请告诉我哪里出了问题。在我的搜索中,我发现了一些信息,这些信息说我不应该使用旧的cmake函数,比如add_compiler_options、include_directories、link_directories、link_libraries。

更新的CMakeList.txt

代码语言:javascript
复制
cmake_minimum_required(VERSION 3.10)
project(cmake)

set(CMAKE_CXX_STANDARD 11)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

add_executable(cmake main.cpp)

target_include_directories(cmake PRIVATE dependencies/GLFW/include)
target_link_libraries(cmake ${PROJECT_SOURCE_DIR}/dependencies/GLFW/lib/libglfw3.a)

新构建错误

代码语言:javascript
复制
C:/mingw-w64/x86_64-7.2.0-posix-seh-rt_v5-rev1/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -ldependencies/GLFW/lib/libglfw3.a
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\cmake.dir\build.make:97: C:/Users/username/CLionProjects/cmake/src/bin/cmake.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:67: CMakeFiles/cmake.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:79: CMakeFiles/cmake.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: cmake] Error 2

参考误差

代码语言:javascript
复制
C:\Users\username\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\181.4668.70\bin\cmake\bin\cmake.exe --build C:\Users\username\CLionProjects\cmake\Build --target cmake -- -j 8
[ 50%] Linking CXX executable C:\Users\username\CLionProjects\cmake\src\bin\cmake.exe
CMakeFiles\cmake.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/username/CLionProjects/cmake/src/main.cpp:9: undefined reference to `glfwInit'
C:/Users/username/CLionProjects/cmake/src/main.cpp:13: undefined reference to `glfwCreateWindow'
C:/Users/username/CLionProjects/cmake/src/main.cpp:16: undefined reference to `glfwTerminate'
C:/Users/username/CLionProjects/cmake/src/main.cpp:21: undefined reference to `glfwMakeContextCurrent'
C:/Users/username/CLionProjects/cmake/src/main.cpp:24: undefined reference to `glfwWindowShouldClose'
C:/Users/username/CLionProjects/cmake/src/main.cpp:27: undefined reference to `__imp_glClear'
C:/Users/username/CLionProjects/cmake/src/main.cpp:30: undefined reference to `glfwSwapBuffers'
C:/Users/username/CLionProjects/cmake/src/main.cpp:33: undefined reference to `glfwPollEvents'
C:/Users/username/CLionProjects/cmake/src/main.cpp:36: undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\cmake.dir\build.make:98: C:/Users/username/CLionProjects/cmake/src/bin/cmake.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:67: CMakeFiles/cmake.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:79: CMakeFiles/cmake.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: cmake] Error 2
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-20 00:35:07

错误消息说,您没有向target_include_directories函数提供正确数量的参数。请参考target_include_directories文档中的语法。

target_include_directories(系统) items1... items2..])

<target>这样的角括号中的参数是必需的,而像[BEFORE]这样的方括号中的参数是可选的。

在您的示例中,目标的名称是cmake,因此包含命令应该如下所示

代码语言:javascript
复制
target_include_directories(cmake PRIVATE "${PROJECT_SOURCE_DIR}/dependencies/GLFW/include")

你可以读到关于${PROJECT_SOURCE_DIR}的文章。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50430253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档