I am trying to explore how to work with Conan through CLion, CMake and everything and so far I have a main project which depends on fmt/10.1.1 and from a personal library. What am I doing wrong here, am I just totally oblivious regarding the mistakes that I am doing or is there something wrong with any of the programs?
The errors that I get are when I am running cmake --build, as follows:
[ 50%] Building CXX object CMakeFiles/exp_proj.dir/main.cpp.o
[100%] Linking CXX executable exp_proj
CMakeFiles/exp_proj.dir/main.cpp.o: In function `main':
main.cpp:(.text.startup+0x18d): undefined reference to `company_name::square(int)'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/exp_proj.dir/build.make:98: exp_proj] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/exp_proj.dir/all] Error 2
gmake: *** [Makefile:136: all] Error 2
MAIN PROJECT: main.cpp - basically, the main project's executable
#include <fmt/core.h>
#include <fmt/color.h>
#include <functions.h>
#include <iostream>
using namespace std;
int main() {
#ifdef NDEBUG
{
fmt::print(fmt::bg(fmt::color::crimson) | fmt::fg(fmt::color::black), "VERSION 1.0");
fmt::print(fmt::fg(fmt::color::crimson), "\n\nRELEASE BUILD...");
fmt::print(" the release build of the version 1.0 of the program runs with no issues!\nThe square of 5 is {}\n\n", company_name::square(5));
}
#else
fmt::print(fmt::bg(fmt::color::cornflower_blue) | fmt::fg(fmt::color::white), "VERSION 1.0");
fmt::print(fmt::fg(fmt::color::cornflower_blue), "\n\nDEBUG BUILD... ");
fmt::print(" the debug build of version 1.0 of the program runs with no issues!\nThe square of 5 is {}\n\n", company_name::square(5));
#endif
return 0;
}
(ignore the fact that I have iostream there, I keep it when I want to do simple troubleshooting)
conanfile.py
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, CMakeDeps, cmake_layout
class project(ConanFile):
name="exp_proj"
version = "1.0"
author = "SIGNORI Gianmario gianmario.signori@company_name.com"
description = "IDK how many project I have done so far because of some unresolvable errors"
topics = ("Conan", "Artifactory", "Libraries")
exports_sources = "main.cpp", "CMakeLists.txt"
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "fPIC": [True, False]}
default_options = {"shared": False, "fPIC": True}
requires = (
"fmt/10.1.1",
"maths/1.0"
)
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
def layout(self):
cmake_layout(self)
def generate(self):
deps = CMakeDeps(self)
deps.generate()
tc = CMakeToolchain(self)
tc.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def package(self):
cmake = CMake(self)
cmake.install()
CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(exp_proj)
find_package(fmt REQUIRED)
find_package(maths REQUIRED)
set(CMAKE_CXX_STANDARD 20)
add_executable(exp_proj main.cpp)
target_link_libraries(exp_proj
PRIVATE
fmt::fmt
maths::maths
)
install(TARGETS exp_proj DESTINATION bin)
and now, the library project, that I guess you have figured out its name is maths:
functions.cpp
#include "functions.h"
namespace company_name
{
int square(int x)
{
return x*x;
}
}
functions.h
#ifndef MATHS_FUNCTIONS_H
#define MATHS_FUNCTIONS_H
namespace company_name{
int square(int x);
}
#endif//MATHS_FUNCTIONS_H
CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(maths)
set(CMAKE_CXX_STANDARD 20)
add_library(maths STATIC functions.cpp)
target_include_directories(maths PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
install(TARGETS maths
ARCHIVE DESTINATION lib
INCLUDES DESTINATION include
)
install(FILES functions.h DESTINATION include)
install(TARGETS maths DESTINATION lib)
conanfile.py
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, CMakeDeps, cmake_layout
from conan.tools.files import copy
class maths(ConanFile):
name="maths"
version="1.0"
author="SIGNORI GIANMARIO gianmario.signori@company_name.com"
exports_sources="CMakeLists.txt", "functions.h", "functions.cpp"
settings="os", "compiler", "build_type", "arch"
options={"shared": [True, False]}
default_options={"shared": False}
def layout(self):
cmake_layout(self)
def generate(self):
deps = CMakeDeps(self)
deps.generate()
tc=CMakeToolchain(self)
tc.generate()
def build(self):
cmake=CMake(self)
cmake.configure()
cmake.build()
def package(self):
#copy(self, "*.h", dst="include", src=self.source_folder)
#copy(self, "*.a", dst="lib", src=self.build_folder)
#copy(self, "*.lib", dst="lib", src=self.build_folder)
#copy(self, "*.cpp", self.source_folder, self.package_folder)
#copy(self, "CMakeLists.txt", self.source_folder, self.package_folder)
cmake = CMake(self)
cmake.install()
#def package_info(self):
# self.cpp_info.libs = ["maths"]
What is wrong? The conan documentation is too abstract to me, you just have to clone their projects, run some commands and that's it, you don't actually understand what a library should contain. I am a beginner and I want to do these by my own so that I can fully understand them.
The commands that I ran are:
$ conan install . -s build_type=...
$ cmake -S . -B build/... -DCMAKE_TOOLCHAIN_FILE=build/.../generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=...
$ cmake --build build/...
$ ./build/.../"exp_proj"
Where ... will be placeholders for Release/Debug builds