我的项目中有两个文件,名为
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 行应该是您所需要的。请参阅我刚刚编写的示例代码,用于测试创建一个然后使用它(在 Ubuntu 16.04 上):
结构.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
我也有同样的问题。我错过的是创建构建文件的位置。
CLion 在
cmake-build-*
目录下创建库或可执行文件。如果Build, Execution, Deployment > CMake > Configuration
是Debug
,则在.a
下创建lib文件(cmake-build-debug
)。
使用命令 cmake . 之后,Makefile 将具有类似 make default_target 的选项,撤销它,然后你的目录中会得到一个 .a 文件。 访问 https://cmake.org/cmake/help/v3.0/command/add_library.html 将为您提供帮助!
它解决了我的问题。我希望建立一个带有 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)`