Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
hsz1273327 committed Jun 11, 2024
1 parent f2d733b commit ff1c77b
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 37 deletions.
Binary file added 工具链/依赖管理/.DS_Store
Binary file not shown.
7 changes: 4 additions & 3 deletions 工具链/依赖管理/hello_sqlite/CmakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ project (hello_sqlite
)
# 引入conan维护的依赖
message(NOTICE "引入conan维护的依赖")
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

# 找到vcpkg安装的sqlite3
find_package(unofficial-sqlite3 CONFIG REQUIRED)

# 找到conan安装的spdlog
find_package(spdlog REQUIRED)

option(AS_STATIC "是否作为纯静态可执行文件编译" off)
# 构造可执行文件的配置
add_executable(${PROJECT_NAME})
Expand All @@ -28,7 +29,7 @@ target_sources(${PROJECT_NAME}
)
target_link_libraries(${PROJECT_NAME}
PRIVATE unofficial::sqlite3::sqlite3
${CONAN_LIBS}
spdlog::spdlog
)
if(AS_STATIC)
message("编译纯静态可执行文件")
Expand Down
5 changes: 3 additions & 2 deletions 工具链/依赖管理/hello_sqlite/conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[requires]
spdlog/1.9.0
spdlog/1.14.1

[generators]
cmake
CMakeDeps
CMakeToolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": 4,
"vendor": {
"conan": {}
},
"include": [
"build/CMakePresets.json"
]
}
18 changes: 10 additions & 8 deletions 工具链/依赖管理/helloworld_with_log/CmakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#项目编译环境
cmake_minimum_required (VERSION 3.17)
# 确保可以描述项目
if (POLICY CMP0048)
cmake_policy(SET CMP0048 NEW)
endif (POLICY CMP0048)
# # 确保可以描述项目
# if (POLICY CMP0048)
# cmake_policy(SET CMP0048 NEW)
# endif (POLICY CMP0048)

project (helloworld_with_log
VERSION 0.0.0
Expand All @@ -12,8 +12,9 @@ project (helloworld_with_log
)
# 引入conan维护的依赖
message(NOTICE "引入conan维护的依赖")
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
# include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
# conan_basic_setup()
find_package(spdlog REQUIRED)

option(AS_STATIC "是否作为纯静态可执行文件编译" off)
# 构造可执行文件的配置
Expand All @@ -22,8 +23,9 @@ add_executable(${PROJECT_NAME})
target_sources(${PROJECT_NAME}
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/helloworld.cc
)
target_link_libraries(${PROJECT_NAME}
${CONAN_LIBS}

target_link_libraries(${PROJECT_NAME}
spdlog::spdlog
)
if(AS_STATIC)
message("编译纯静态可执行文件")
Expand Down
33 changes: 33 additions & 0 deletions 工具链/依赖管理/helloworld_with_log/CmakeLists.txt.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#项目编译环境
cmake_minimum_required (VERSION 3.17)
# 确保可以描述项目
if (POLICY CMP0048)
cmake_policy(SET CMP0048 NEW)
endif (POLICY CMP0048)

project (helloworld_with_log
VERSION 0.0.0
DESCRIPTION "简单测试"
LANGUAGES CXX
)
# 引入conan维护的依赖
message(NOTICE "引入conan维护的依赖")
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

option(AS_STATIC "是否作为纯静态可执行文件编译" off)
# 构造可执行文件的配置
add_executable(${PROJECT_NAME})
## 设置源码位置
target_sources(${PROJECT_NAME}
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/helloworld.cc
)
target_link_libraries(${PROJECT_NAME}
${CONAN_LIBS}
)
if(AS_STATIC)
message("编译纯静态可执行文件")
target_compile_options(${PROJECT_NAME}
PRIVATE "-static"
)
endif()
5 changes: 3 additions & 2 deletions 工具链/依赖管理/helloworld_with_log/conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[requires]
spdlog/1.9.0
spdlog/1.14.1

[generators]
cmake
CMakeDeps
CMakeToolchain
72 changes: 50 additions & 22 deletions 工具链/依赖管理/可执行文件项目依赖管理.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ C/C++项目的依赖管理可以遵从如下规则进行:
+ `CMAKE_C_COMPILER=gcc`
+ `CMAKE_Fortran_COMPILER=gfortran`

conan则主要要设置它名为`default``profile`.所谓`profile`可以理解为编译环境设置通过`RUN conan profile new <profile_name> --detect`创建,通过`RUN conan profile update <key>=<value> <profile_name>`设置.可以设置的项可以在[这里查到](https://docs.conan.io/en/latest/reference/profiles.html)
conan则主要要设置它名为`default``profile`.所谓`profile`可以理解为编译环境设置,通过`conan profile detect`创建,通过`conan profile update <key>=<value> <profile_name>`设置.默认位置在`~/.conan2/profiles/default`文件中保存

可以设置的项可以在[这里查到](https://docs.conan.io/2/reference/config_files/profiles.html)

通常我们比较关系如下几个设置项:
+ settings.compiler=gcc
Expand All @@ -55,30 +57,54 @@ C/C++项目的依赖管理可以遵从如下规则进行:
+ settings.compiler.threads=posix
+ settings.build_type=Release

2. 项目根目录编写[conanfile.txt](https://docs.conan.io/en/latest/reference/conanfile_txt.html)用于描述依赖.
2. 项目根目录编写[conanfile.txt](https://docs.conan.io/2/reference/conanfile_txt.html)用于描述依赖.

`conanfile.txt`中通常只要设置好`requires``generators`即可.

其中`requires`中以`<package_name>/<version>`的形式描述依赖,我们可以通过`conan search <package_name>[ -r <REMOTE>]`来查找conan仓库中托管的依赖包.默认会从[conancenter](https://center.conan.io)中查找包.如果不喜欢命令行查找,也可以去网页<https://conan.io/center/>上直接搜索,我们也可以用`-r`来指定已经设置过的conan仓库,私有仓库一块我们后面介绍.
其中`requires`中以`<package_name>/<version>`的形式描述依赖,我们可以通过`conan search <package_name>[ -r <REMOTE>]`来查找conan仓库中托管的依赖包.默认会从[conancenter](https://conan.io/center)中查找包.我们也可以用`-r`来指定已经设置过的conan仓库,私有仓库一块我们后面介绍.

3. 创建`build`文件夹,进入后执行`conan install ..[ --build=missing[ --profile=<profile_name>[ -r <REMOTE>]]]`
其中`generators`目前基本都是填的

```txt
[generators]
CMakeDeps
CMakeToolchain
```
3. 创建`build`文件夹,执行`conan install . --output-folder=build [ --build=missing[ --profile=<profile_name>[ -r <REMOTE>]]]`安装
`conan install`命令用于安装并配置项目的依赖.默认只会下载当前平台已经编译好的二进制库,如果碰到二进制库有缺失就会失败,我们可以加上`--build=missing`让它碰到有缺失的库时尝试现编译.而`--profile`则是指定特定的编译配置项
4. 在项目根目录创建你的cmake配置文件`CmakeLists.txt`,并在其中加入对conan管理包的引用:
```cmake
...
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
find_package(<依赖包名> REQUIRED)
...
target_link_libraries(${PROJECT_NAME}
...
${CONAN_LIBS}
<依赖包名>::<依赖包名>
)
```
5. 回到`build`文件夹,然后执行`cmake --build ..`
具体怎么导入依赖,我们通常可以在`conancenter`上对应仓库的说明中找到
5. 在`build`文件夹下,根据执行操作系统的不同,执行如下命令:
+ windows下:
```bash
cmake .. -G "Visual Studio 15 2017" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
cmake --build . --config Release
```
+ linux/macos下
```bash
cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
cmake --build .
```
这样就编译完成了
我们修改前面的`helloworld_with_log`继续做例子,这个例子在<https://github.com/hsz1273327/TutorialForCLang/tree/master/examples/%E5%B7%A5%E5%85%B7%E9%93%BE/%E7%BC%96%E8%AF%91%E5%B7%A5%E5%85%B7%E9%93%BE/%E4%BE%9D%E8%B5%96%E7%AE%A1%E7%90%86/helloworld_with_log>看到
Expand All @@ -88,21 +114,22 @@ C/C++项目的依赖管理可以遵从如下规则进行:
```conf
[requires]
spdlog/1.9.0
spdlog/1.14.1
[generators]
cmake
CMakeDeps
CMakeToolchain
```
+ `CmakeLists.txt`
```cmake
#项目编译环境
cmake_minimum_required (VERSION 3.17)
# 确保可以描述项目
if (POLICY CMP0048)
cmake_policy(SET CMP0048 NEW)
endif (POLICY CMP0048)
# # 确保可以描述项目
# if (POLICY CMP0048)
# cmake_policy(SET CMP0048 NEW)
# endif (POLICY CMP0048)
project (helloworld_with_log
VERSION 0.0.0
Expand All @@ -111,8 +138,9 @@ C/C++项目的依赖管理可以遵从如下规则进行:
)
# 引入conan维护的依赖
message(NOTICE "引入conan维护的依赖")
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
# include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
# conan_basic_setup()
find_package(spdlog REQUIRED)
option(AS_STATIC "是否作为纯静态可执行文件编译" off)
# 构造可执行文件的配置
Expand All @@ -121,9 +149,9 @@ C/C++项目的依赖管理可以遵从如下规则进行:
target_sources(${PROJECT_NAME}
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/helloworld.cc
)
target_link_libraries(${PROJECT_NAME}
PRIVATE unofficial::sqlite3::sqlite3
${CONAN_LIBS}
target_link_libraries(${PROJECT_NAME}
spdlog::spdlog
)
if(AS_STATIC)
message("编译纯静态可执行文件")
Expand Down Expand Up @@ -173,11 +201,11 @@ project (hello_sqlite
)
# 引入conan维护的依赖
message(NOTICE "引入conan维护的依赖")
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
# 找到vcpkg安装的sqlite3
find_package(unofficial-sqlite3 CONFIG REQUIRED)
# 找到conan安装的spdlog
find_package(spdlog REQUIRED)
option(AS_STATIC "是否作为纯静态可执行文件编译" off)
# 构造可执行文件的配置
Expand All @@ -188,7 +216,7 @@ target_sources(${PROJECT_NAME}
)
target_link_libraries(${PROJECT_NAME}
PRIVATE unofficial::sqlite3::sqlite3
${CONAN_LIBS}
spdlog::spdlog
)
if(AS_STATIC)
message("编译纯静态可执行文件")
Expand Down

0 comments on commit ff1c77b

Please sign in to comment.