Цитата(proc_maker @ 22.9.2010, 13:05) | Всем привет!
Как получить строку в коде C++ из опции cmake говорящей ему куда ставить пакет?
Например, если я запускаю:
cmake CMAKE_INSTALL_PREFIX=/tmp ..
потом
make
то во время компиляции хочется каким-то образом получить со значением которое я передал через CMAKE_INSTALL_PREFIX, т.е.
Спасибо заранее, Дмитрий |
Решение делается через CMake Вот пример решения - config.h.in
Код | #ifndef __CONFIG_H #define __CONFIG_H
// This line will be parsed by cmake and replaced to install prefix of project #define INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}"
#endif // __CONFIG_H
|
main.c
Код | // System includes #include <stdio.h>
// Internal project includes #include "config.h"
// Program Enter Point int main(int argc, char** argv) { // Print our install prefix printf("%s\r\n", INSTALL_PREFIX); return 0; }
|
CMakeLists.txt
Код | cmake_minimum_required(VERSION 2.6)
# project name SET(PROJECT_NAME "test")
# configuration file SET(CONFIG_FILE "config.h.in")
# Sources SET(project_SOURCES main.c)
# this line will configure our config file and replace all CMAKE variables to correct ones CONFIGURE_FILE(${CONFIG_FILE} config.h)
# we need to include BUILD directory for including just compiled config.h INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
# compile our test add_executable(${PROJECT_NAME} ${project_SOURCES})
|
Пример использования
Код | /work/test # mkdir build /work/test # cd build/ /work/test/build # cmake .. -DCMAKE_INSTALL_PREFIX=/tmp -- The C compiler identification is GNU -- The CXX compiler identification is GNU -- Check for working C compiler: /usr/bin/gcc -- Check for working C compiler: /usr/bin/gcc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Configuring done -- Generating done -- Build files have been written to: /work/test/build /work/test/build # make Scanning dependencies of target test [100%] Building C object CMakeFiles/test.dir/main.c.o Linking C executable test [100%] Built target test /work/test/build # ./test /tmp
|
Надеюсь помог.
|