cmake-创建一个静态库
CMake - Creating a static library
我在项目中有两个文件,称为 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 IDE。
add_library Line应该是您所需要的。请参阅此示例代码我刚刚写的是为了测试创建一个,然后使用它(在Ubuntu 16.04上(:
structure.h:
int sum( int a, int b );
structure.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 %dn", 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
我也有同样的问题。我错过的是创建构建文件的位置。
Clion在cmake-build-*
目录下制作库或Excectrables。如果Build, Execution, Deployment > CMake > Configuration
是Debug
,则在cmake-build-debug
中创建LIB文件(.a
(。
在使用命令 cmake。之后> ,撤销它,然后在r目录中获得.a文件。访问https://cmake.org/cmake/help/v3.0/command/add_library.html将为您提供帮助!
相关文章:
- 分离一个静态常量 std::thread?
- 从另一个静态常量数组初始化静态常量数组(只需少量计算)
- 如果两个静态库使用相同的另一个静态库,如何避免"LNK2005 Already Defined error"?
- 我可以使用 constexpr 函数声明一个静态数组吗?
- 我正在尝试在我的类中创建一个静态成员,但编译器警告我它是未定义的
- 一个静态变量多副本
- 两个C++库如何共享一个静态缓冲区?
- 为什么静态指针返回函数中有一个"静态"键?
- C++ 给出编译器错误,将一个(静态)常量全局变量设置为另一个静态常量变量
- cmake-创建一个静态库
- 对另一个静态库中静态库方法的未定义引用
- 如何创建一个静态类来引用带有C++的字符串变量
- QMAKE:将静态库链接到另一个静态库
- 长镜头 -- 可能有一个静态类成员,该类成员具有访问非静态成员的 lambda
- 为 c++ 构建一个静态库,该库在内部依赖于第三方库
- 使用类模板参数推导来创建一个静态接口
- 具有两个动态维度和一个静态维度的多维数组
- 创建一个静态包含ffmpeg的共享库
- NetBeans创建一个静态库C
- 在抽象类中,可以使用抽象类A的类型创建一个静态字段