C语言多文件的工程结构,c++ - CMake项目结构:如何正确合并库并将它们包含在多个可执行文件中 - 堆栈内存溢出...

我正在建立一个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,我只是想找出解决此问题的最佳方法。

谢谢你的帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值