我正在建立一个C项目,目前我对如何正确链接库有些迷惑。
总结一下:我将有3个项目,这些项目将具有多个可执行文件。 这些项目将使用来自具有多个目录(子库)的库中的代码。 我在include目录中包含了该项目的所有include。 我还将为src和lib提供一个测试目录。
如何在单独目录中的库中链接?
有没有一种方法可以将所有这些库合并到一个主库中,所以我只需要链接一个库?
结构如下:
Project:
|-- bin
|-- build
|-- src
| |-- Project 1
| | |-- Project1a.c
| | |-- Project1b.c
| | |-- cmakelists.txt
| |-- Project 2
| | |--Project2.c
| |-- Project 3
| | |--Project3.c
| |-- cmakelists.txt
|
|-- lib
| |--Crypto_Lib
| | |-- Crypto.c
| | |-- cmakelists.txt
| |--Communications_Lib
| | |-- Communications.c
| | |-- cmakelists.txt
| |--Error_Lib
| | |-- Error.c
| | |-- cmakelists.txt
| |--cmakelists.txt
|
|-- include
| |--src
| | |-- Project 1
| | | |-- Project1a.h
| | | |-- Project1b.h
| | |-- Project 2
| | | |-- Project2.h
| | |-- Project 3
| | |-- Project3.h
| |--lib
| | |--Hash
| | | |-- Crypto.h
| | |--Communications
| | | |-- Communications.h
| | |--Error
| | | |-- Error.h
|
|-- test
| |--src (etc...)
| |--lib (etc...)
| |--cmakelists.txt
|
|--cmakelists.txt
我目前有:
〜/ CMakeLists.txt
add_subdirectory(lib)
add_subdirectory(src)
add_subdirectory(test)
〜/ lib / CMakeLists.txt
#To access the header file
include_directories(${CMAKE_SOURCE_DIR}/includes/lib)
add_subdirectory(communications)
add_subdirectory(crypto)
add_subdirectory(error)
〜/ src / CMakeLists.txt
#To access the header file
include_directories(${CMAKE_SOURCE_DIR}/includes/src)
add_subdirectory(project_one)
add_subdirectory(project_two)
add_subdirectory(project_three)
〜/ lib / communications / CMakeLists.txt
add_library(Comm_Lib STATIC communications.c)
〜/ lib / crypto /CMakeLists.txt
add_library(Crypto _Lib STATIC crypto.c)
〜/ lib / error / CMakeLists.txt
add_library(Error_Lib STATIC error.c)
〜/ src / project_one / CMakeLists.txt
add_executable(Project_1A Project_1a.c)
add_executable(Project_1B Project_1B.c)
#target_link_library(Project_1A Full_Library) Would like to be able to join all libraries together
〜/ src / project_two / CMakeLists.txt
add_executable(Project_Three Project_two.c)
〜/ src / project_one / CMakeLists.txt
add_executable(Project_Three Project_three.c)
〜/ test / CMakeLists.txt
add_subdirectory(lib_test)
add_subdirectory(src_test)
**Haven’t gotten any deeper in the test CMakeLists.txt
我知道我还没有使用过link_libraries,我只是想找出解决此问题的最佳方法。
谢谢你的帮助!