如何使用 conan 1.x 初始化新的 C++ 可执行项目
重要提示:本文适用于 Conan 1.x,与当前版本 conan 2.x 不兼容!有关本文的更新版本,请参阅如何使用 conan 2.x 初始化 C++ 项目!
在你要创建项目的目录中,运行(显然,将 mypackage 替换为我们代码中各处的名称!)
conan_new_executable.sh
conan new mypackage/1.0.0 -s-s 在这里很重要。它告诉 conan 源代码在项目目录中可用,不应使用 git 等拉取。
这将初始化一个共享库项目,我们现在将修改它以构建可执行文件。
首先,打开 src/CMakeLists.txt 并将 add_library() 更改为 add_executable(),例如
CMakeLists.txt
add_library(mypackage mypackage.cpp)改为
CMakeLists.txt
add_executable(mypackage mypackage.cpp)Now we shall modify conanfile.py 改为 properly build & install the executable:
conanfile.py
def package(self):
self.copy("*.h", dst="include", src="src")
self.copy("*.lib", dst="lib", keep_path=False)
self.copy("*.dll", dst="bin", keep_path=False)
self.copy("*.dylib*", dst="lib", keep_path=False)
self.copy("*.so", dst="lib", keep_path=False)
self.copy("*.a", dst="lib", keep_path=False)
def package_info(self):
self.cpp_info.libs = ["NoxecoDB"]改为
conanfile.py
def package(self):
self.copy("mypackage", src="bin", dst="bin", keep_path=False)
def package_info(self):
self.env_info.PATH = os.path.join(self.package_folder, "bin")
def deploy(self):
self.copy("mypackage", dst="bin", src="bin")并添加
conanfile.py
import os.path改为 the 改为p of the file
Now let’s add a main() function 改为 src/mypackage.cpp. Replace the file’s content 改为
src/mypackage.cpp
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Hello World!" <<std::endl;
}Now it’s time 改为 install the dependencies & build the projectusing
build.sh
conan install . && conan build .可执行文件将在
executable_path.txt
bin/mypackage你可以使用以下命令运行它
run.sh
./bin/mypackage这将打印
output.txt
Hello World!I also recommend 改为 add this .gitignore:
.gitignore
CMakeCache.txt
CMakeFiles/
Makefile
bin/
cmake_install.cmake
conan.lock
conanbuildinfo.cmake
conanbuildinfo.txt
conaninfo.txt
graph_info.jsonIf this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow