Can I replace a specific character with another CMake character?

I am trying to make an executable with CMake. This executable requires the full path to the file that is in the project.

int main()
{
    std::ifstream("fullpath_to_file");
    //....
}

I think that if CMake can replace certain characters in the source code with a user-specified string, there will be no need to fully encode the full path.

For example, if CMake can replace ${CMAKE_PROJECT_DIR}the source code (cpp) with a cmake variable, for example $ {PROJECT_SOURCE_DIR}, then I can write a source like this.

int main()
{
    std::ifstream("${CMAKE_PROJECT_DIR}/input/my_input.txt");
    //....
}

Is there any way to do this?

Thanks.

+4
source share
2 answers

CMake , , - . -, , say config.h.in , CMake , , config.h.in

#ifndef __CONFIG_H__
#define __CONFIG_H__

#define PROJECT_DIR @CMAKE_PROJECT_DIR@

#endif

CMakeLists.txt @ cmake. , CMakeLists.txt :

include_directories(${CMAKE_BINARY_DIR})
config_file(
    ${CMAKE_SOURCE_DIR}/config.h.in
    ${CMAKE_BINARY_DIR}/config.h
)

CMake config.h , . , : config.h ++ PROJECT_DIR, .

+7

, CMake . , .

- ++ CMake - FOO_PATH ++

  • pass -DFOO_PATH=<path_to_foo> CMake.
  • #cmakedefine FOO_PATH FOO_PATH config.h, FOO_PATH CMake .
+5

Source: https://habr.com/ru/post/1620341/


All Articles