R
R
RR_Zz2021-03-04 02:16:01
C++ / C#
RR_Zz, 2021-03-04 02:16:01

How to add a configuration file to a library?

I am writing a library in C; it has a configuration file conf.h.
I want the user to be able to override the settings of this file in his project using user_conf.h
The problem is that I can't figure out how to make the library see the user's configuration file.
I tried using CMake to shove user_conf.h into the lib / inc folder, it did not work.

Define will be redefined, no more is needed, and I would also like to avoid the -D compilation option, there are a lot of parameters.

./conf.h

#ifndef CONF_H_
#define CONF_H_

#include "user_conf.h"

#ifndef PARAM1
#define PARAM1 1
#endif

#ifndef PARAM2
#define PARAM2 2
#endif

#endif

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2021-03-04
@RR_Zz

This is not usually how things like this are done.
To do this, cmake provides the configure_file
function. The point is that you create a template file containing references to cmake variables. configure_file processes this template, replaces all references with variable values, and creates a new file from them. This new file is what you use in the project. Those. the real file (in your case conf.h) does not exist in the project, it only appears at compile time.
The user can change the values ​​of cmake variables through the cmake command line parameters using the -D switch, for example: cmake ... -DMY_VAL="New value".
A similar variable inside cmakelists.txt is usually declared through an option, where you can also specify a variable description and a default value.
You can find usage examples in any more or less large cmake project on github.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question