CMake - 创建静态库

44

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

Structure.h Structure.c

我想创建一个静态库,可以被其他希望使用这些文件的项目加载。这是当前的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})

我使用那个CMake文件进行构建时,没有生成静态库。什么都没有发生。我做错了什么吗?

我正在使用CLion集成开发环境。

4个回答

66

add_library这一行应该就是你所需要的。看看我刚写的这个示例代码,测试创建一个库并使用它(在 Ubuntu 16.04 上):

Structure.h:

int sum( int a, int b );

结构体.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:

# 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 )

然后这里是运行它的输出结果:

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

2
这解决了问题吗?我想知道更多,因为我有类似的问题。我不明白这与 OP 的代码有什么不同。 - kutschkem

6

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

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


1

1
它解决了我的问题。我想要用C++ 23客户端和静态库建立一个CLion项目。非常感谢你的帖子,因为在阅读后,我在几分钟内就让我的东西运行起来了。出于某种原因,我觉得CMake不直观,尽管有很多参考资料,但我仍然缺乏关于如何做简单事情的直接指导,而这些在"make"中很容易。不管怎样,谢谢。这是我最终得到的结果:
`cmake_minimum_required(VERSION 3.26)
project(vpa)
set(CMAKE_CXX_STANDARD 23)

#########################
# Regarding the Library #
#########################
add_library(vpad STATIC vpad.cpp
        vpad.cpp        vpad.h)

################################
# Regarding the Client Program #
################################
add_executable(vpa main.cpp
        vpa.cpp         vpa.h
        vpad.h
)
target_link_libraries(vpa vpad)`

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接