R
R
Rag'n' Code Man2021-11-30 12:55:08
CMake
Rag'n' Code Man, 2021-11-30 12:55:08

What in CMAKE should I consider a library?

I have a C++ project with the following structure:

include/
lib/
src/

In srcI put all the CPPfiles. But I also needed the tinyfiledialogs
library . I put its files in lib/, and in main.cppI connected it like this

#include "../lib/tinyfiledialogs/tinyfiledialogs.h"

Now, in order for me to compile this with, g++I just need to add this library to the command arguments.

g++ src/main.cpp lib/tinyfiledialogs/tinyfiledialogs.h

There is a directive in cmake add_library(). In what cases should I use it and should I use it in this case? Maybe just put all the files in add_executable()?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2021-11-30
@iDmitriyWinX

In general, libraries are usually installed separately into the system (for Linux) using a standard package manager.
If your library is not in the package manager, then you can build from source.
In this case, you can just "put" all the files in add_executable. Nobody can stop you from doing this.
But it's usually easier, better, and more versatile for third-party libraries to create their own targets with add_library and link them to the main target with target_link_libraries. The library can be made both dynamic and static.
If the project is quite spread out and contains, for example, tests and / or several executable files, then in general it is possible to create a separate static library in each project directory. On large projects, this can often save compilation time, due to the fact that ready-made libraries will be linked to several final targets, and not rebuilt from source each time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question