CMake -创建静态库

iih3973s  于 2023-10-16  发布在  其他
关注(0)|答案(4)|浏览(127)

在我的项目中有两个名为Test4的文件:
Structure.hStructure.c
我想创建一个静态库,可以加载的其他项目谁想要使用这些文件。以下是我目前的CMake文件:

  1. cmake_minimum_required(VERSION 3.6)
  2. project(Test4)
  3. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  4. set(SOURCE_FILES Structure.c Structure.h)
  5. add_library(Test4 STATIC ${SOURCE_FILES})

当我使用该CMake文件进行构建时,不会生成任何静态库。什么都没发生。我做错什么了吗?
我正在使用CLion IDE。

oknwwptz

oknwwptz1#

add_library行应该是您所需要的全部。请看我刚刚写的这个示例代码,以测试创建一个并使用它(在Ubuntu 16.04上):
结构h:

  1. int sum( int a, int b );

结构c:

  1. int sum( int a, int b ) {
  2. return a + b;
  3. }

Main.c:

  1. #include <stdio.h>
  2. #include "Structure.h"
  3. int main() {
  4. int a = 5;
  5. int b = 8;
  6. int c = sum( a, b );
  7. printf( "sum of %d and %d is %d\n", a, b, c );
  8. return 0;
  9. }

CMakeLists.txt:

  1. # CMake instructions to make the static lib
  2. ADD_LIBRARY( MyStaticLib STATIC
  3. Structure.c )
  4. # CMake instructions to test using the static lib
  5. SET( APP_EXE StaticTest )
  6. ADD_EXECUTABLE( ${APP_EXE}
  7. Main.c )
  8. TARGET_LINK_LIBRARIES( ${APP_EXE}
  9. MyStaticLib )

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

  1. nick@dusseldorf:~/code/cmake/static_lib$ ls
  2. CMakeLists.txt Main.c Structure.c Structure.h
  3. nick@dusseldorf:~/code/cmake/static_lib$ cmake .
  4. -- The C compiler identification is GNU 5.4.0
  5. -- The CXX compiler identification is GNU 5.4.0
  6. -- Check for working C compiler: /usr/bin/cc
  7. -- Check for working C compiler: /usr/bin/cc -- works
  8. -- Detecting C compiler ABI info
  9. -- Detecting C compiler ABI info - done
  10. -- Detecting C compile features
  11. -- Detecting C compile features - done
  12. -- Check for working CXX compiler: /usr/bin/c++
  13. -- Check for working CXX compiler: /usr/bin/c++ -- works
  14. -- Detecting CXX compiler ABI info
  15. -- Detecting CXX compiler ABI info - done
  16. -- Detecting CXX compile features
  17. -- Detecting CXX compile features - done
  18. -- Configuring done
  19. -- Generating done
  20. -- Build files have been written to: /home/nick/code/cmake/static_lib
  21. nick@dusseldorf:~/code/cmake/static_lib$ ls
  22. CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt Main.c Makefile Structure.c Structure.h
  23. nick@dusseldorf:~/code/cmake/static_lib$ make
  24. Scanning dependencies of target MyStaticLib
  25. [ 25%] Building C object CMakeFiles/MyStaticLib.dir/Structure.c.o
  26. [ 50%] Linking C static library libMyStaticLib.a
  27. [ 50%] Built target MyStaticLib
  28. Scanning dependencies of target StaticTest
  29. [ 75%] Building C object CMakeFiles/StaticTest.dir/Main.c.o
  30. [100%] Linking C executable StaticTest
  31. [100%] Built target StaticTest
  32. nick@dusseldorf:~/code/cmake/static_lib$ ls
  33. CMakeCache.txt cmake_install.cmake libMyStaticLib.a Makefile Structure.c
  34. CMakeFiles CMakeLists.txt Main.c StaticTest Structure.h
  35. nick@dusseldorf:~/code/cmake/static_lib$ ./StaticTest
  36. sum of 5 and 8 is 13
展开查看全部
xwbd5t1u

xwbd5t1u2#

我也有同样的问题。我错过的是创建构建文件的位置。
CLion在cmake-build-*目录下创建库或可执行文件。如果Build, Execution, Deployment > CMake > ConfigurationDebug,则在cmake-build-debug下创建lib文件(.a)。

b0zn9rqh

b0zn9rqh3#

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

ffvjumwh

ffvjumwh4#

它解决了我的问题。我希望建立一个CLion项目,使用C++ 23客户端和静态库。我很感谢你的帖子,因为在我读了它之后,我在几分钟内就完成了我的工作。出于某种原因,我发现CMake是反直觉的,尽管有很多参考资料,我仍然缺乏关于如何做简单的事情的直接指导,这些事情在“make”中很容易。以下是我最终得到的结果:

  1. `cmake_minimum_required(VERSION 3.26)
  2. project(vpa)
  3. set(CMAKE_CXX_STANDARD 23)
  4. #########################
  5. # Regarding the Library #
  6. #########################
  7. add_library(vpad STATIC vpad.cpp
  8. vpad.cpp vpad.h)
  9. ################################
  10. # Regarding the Client Program #
  11. ################################
  12. add_executable(vpa main.cpp
  13. vpa.cpp vpa.h
  14. vpad.h
  15. )
  16. target_link_libraries(vpa vpad)`
展开查看全部

相关问题