How to initialize new C++ executable project using conan 1.x
Important note: This post is for Conan 1.x and is not compatible with the current version conan 2.x! See How to initialize C++ project using conan 2.x for an updated version of this post!
In the directory where you want to create the project, run (obviously, replace mypackage
by the name of everywhere in our code!)
conan new mypackage/1.0.0 -s
-s
is important here. It tells conan that the source is available in the project directory and it should not be pulled using git etc.
This will initialize a shared library project, which we will now modify to build an executable.
First, open src/CMakeLists.txt
and change add_library()
to add_executable()
, for example
add_library(mypackage mypackage.cpp)
to
add_executable(mypackage mypackage.cpp)
Now we shall modify conanfile.py
to properly build & install the executable:
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"]
with
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")
and add
import os.path
to the top of the file
Now let’s add a main()
function to src/mypackage.cpp
. Replace the file’s content with
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Hello World!" <<std::endl;
}
Now it’s time to install the dependencies & build the projectusing
conan install . && conan build .
The executable will be in
bin/mypackage
You can run it using
./bin/mypackage
which will print
Hello World!
I also recommend to add this .gitignore
:
CMakeCache.txt
CMakeFiles/
Makefile
bin/
cmake_install.cmake
conan.lock
conanbuildinfo.cmake
conanbuildinfo.txt
conaninfo.txt
graph_info.json