C++ CMake - 创建一个静态库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43772887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:34:52  来源:igfitidea点击:

CMake - Creating a static library

c++cbuildcmakeclion

提问by Hatefiend

I have two files in my project called Test4:

我的项目中有两个文件,名为Test4

Structure.hStructure.c

Structure.hStructure.c

I want to create a static library that can be loaded by other projects who want to use those files. Here is my CMake file currently:

我想创建一个静态库,可以由想要使用这些文件的其他项目加载。这是我目前的 CMake 文件:

cmake_minimum_required(VERSION 3.6)
project(Test4)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES Structure.c Structure.h)
add_library(Test4 STATIC ${SOURCE_FILES})

When I build using that CMake file, no static library is generated. Nothing happens. Am I doing something wrong?

当我使用该 CMake 文件构建时,不会生成任何静态库。没发生什么事。难道我做错了什么?

I am using the CLion IDE.

我正在使用 CLion IDE。

回答by ndeubert

The add_library line should be all you need. See this example code I just wrote to test out creating one and then using it (on Ubuntu 16.04):

add_library 行应该是您所需要的。请参阅我刚刚编写的示例代码来测试创建一个然后使用它(在 Ubuntu 16.04 上):

Structure.h:

结构.h:

int sum( int a, int b );

Structure.c:

结构.c:

int sum( int a, int b ) { 
    return a + b;
}

Main.c:

主文件:

#include <stdio.h>
#include "Structure.h"

int main() {
    int a = 5;
    int b = 8;
    int c = sum( a, b );

    printf( "sum of %d and %d is %d\n", a, b, c );

    return 0;
}

CMakeLists.txt:

CMakeLists.txt:

# CMake instructions to make the static lib

ADD_LIBRARY( MyStaticLib STATIC
             Structure.c )


# CMake instructions to test using the static lib

SET( APP_EXE StaticTest )

ADD_EXECUTABLE( ${APP_EXE}
                Main.c ) 

TARGET_LINK_LIBRARIES( ${APP_EXE}
                       MyStaticLib )

And then here is the output from running it:

然后这是运行它的输出:

nick@dusseldorf:~/code/cmake/static_lib$ ls
CMakeLists.txt  Main.c  Structure.c  Structure.h

nick@dusseldorf:~/code/cmake/static_lib$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/nick/code/cmake/static_lib

nick@dusseldorf:~/code/cmake/static_lib$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  Main.c  Makefile  Structure.c  Structure.h

nick@dusseldorf:~/code/cmake/static_lib$ make
Scanning dependencies of target MyStaticLib
[ 25%] Building C object CMakeFiles/MyStaticLib.dir/Structure.c.o
[ 50%] Linking C static library libMyStaticLib.a
[ 50%] Built target MyStaticLib
Scanning dependencies of target StaticTest
[ 75%] Building C object CMakeFiles/StaticTest.dir/Main.c.o
[100%] Linking C executable StaticTest
[100%] Built target StaticTest

nick@dusseldorf:~/code/cmake/static_lib$ ls
CMakeCache.txt  cmake_install.cmake  libMyStaticLib.a  Makefile    Structure.c
CMakeFiles      CMakeLists.txt       Main.c            StaticTest  Structure.h

nick@dusseldorf:~/code/cmake/static_lib$ ./StaticTest 
sum of 5 and 8 is 13

回答by noobar

I had same issue. What I missed is the location where build files are created.

我有同样的问题。我错过的是创建构建文件的位置。

CLion makes libraries or exectables under cmake-build-*directory. IfBuild, Execution, Deployment > CMake > Configurationis Debug, the lib file (.a) is created under cmake-build-debug.

CLion 在cmake-build-*目录下制作库或可执行文件。如果Build, Execution, Deployment > CMake > ConfigurationDebug.a则在cmake-build-debug.

回答by Layne Liu

After you use command cmake .Then the Makefile will have the option like make default_target, revoke it and then you get a .a file in you r directory. Visit https://cmake.org/cmake/help/v3.0/command/add_library.htmlwill help you!

使用命令cmake 后。然后 Makefile 将有像make default_target这样的选项,撤销它,然后你会在你的 r 目录中得到一个 .a 文件。访问https://cmake.org/cmake/help/v3.0/command/add_library.html会帮助你!