简体   繁体   English

在cmake中包含静态库

[英]Include static library with cmake

I am in the process of learning C++. 我正在学习C ++。 One of the most confusing things is to learn how to link libraries when using CMake (I use CLion on Windows which uses CMake ). 其中最令人困惑的事情是学习如何使用CMake的(我在Windows上使用克利翁它使用CMake的 ),当链接库。 I downloaded the the pre-built binaries for GLFW from here . 我从这里下载了GLFW的预编译二进制文件。 I grabbed the includes folder and the library folder built with mingw. 我抓住了包含文件夹和用mingw构建的库文件夹。 I places the necessary files in a dependencies folder in my project. 我将必要的文件放在项目的依赖文件夹中。 My folder structure looks like this. 我的文件夹结构如下所示。

Folder Structure 资料夹结构

Project
├── bin
├── build
└── src
    ├── CMakeLists.txt
    ├── dependencies
    │   └── GLFW
    │       ├── include
    │       │   └── GLFW
    │       │       ├── glfw3.h
    │       │       └── glfw3native.h
    │       └── lib
    │           └── libglfw3.a
    ├── lib
    └── main.cpp

Code: (Starter code pulled from GLFW Documentation here ) 代码:( 此处是从GLFW文档中提取的入门代码)

#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 CmakeList.txt

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)

Error 错误

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]

Please tell me where I am going wrong. 请告诉我我要去哪里了。 In my search I came across some information that said I shouldn't be using older cmake functions like add_compiler_options, include_directories, link_directories, link_libraries. 在搜索中,我发现一些信息说我不应该使用旧的cmake函数,例如add_compiler_options,include_directories,link_directories,link_libraries。

Updated CMakeList.txt 更新了CMakeList.txt

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)

New Build Error 新建错误

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

Reference Error 参考误差

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

The error message says that you have not supplied correct number of arguments to the target_include_directories function. 错误消息表明您没有为target_include_directories函数提供正确数量的参数。 Please refer to the syntax of target_include_directories in the documentation . 请参考文档target_include_directories的语法。

target_include_directories(<target> [SYSTEM] [BEFORE] target_include_directories(<target> [系统] [之前]
<INTERFACE|PUBLIC|PRIVATE> [items1...] [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...]) <INTERFACE | PUBLIC | PRIVATE> [项目1 ...] [<INTERFACE | PUBLIC | PRIVATE> [项目2 ...] ...])

The arguments in angular brackets like <target> are required and the ones in square brackets like [BEFORE] are optional. 尖括号中的参数(例如<target>是必需的,方括号中的参数(例如[BEFORE]是可选的。

In your example the name of the target is cmake and hence the include command should look like 在您的示例中,目标名称为cmake ,因此include命令应类似于

target_include_directories(cmake PRIVATE "${PROJECT_SOURCE_DIR}/dependencies/GLFW/include")

You can read about ${PROJECT_SOURCE_DIR} . 您可以阅读有关${PROJECT_SOURCE_DIR}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM