[CMake] Setting CMAKE_CXX_COMPILER & CMAKE_C_COMPILER in CMakeLists.txt

aditya siram aditya.siram at gmail.com
Mon Oct 21 11:05:34 EDT 2013


Hi all,
I'm using CMake to try compile a set of C bindings to the FLTK GUI library
and would like to set the C/C++ compiler variables in the build script but
haven't been successful. I realize that setting  environment variables is
the recommended method but I've tried to explain why I need it below.

The FLTK library also builds an executable called `fltk-config` that can be
queried for information about the FLTK build. For examples, to find out
what C++ compiler was used to build FLTK I can just do:
fltk-config --cxx

I'd like to use the output of these queries to set the CMAKE_CXX_COMPILER
and CMAKE_C_COMPILER variables.

Here's what I have so far:
find_program(FLTKCONFIG fltk-config)
if(NOT FLTKCONFIG)
   message(FATAL_ERROR "Cannot find the fltk-config executable. Is FLTK
installed?")
endif(NOT FLTKCONFIG)
execute_process(COMMAND ${FLTKCONFIG} --cxx OUTPUT_VARIABLE
FLTK_CXX_COMPILER_QUERY_RESULT)
execute_process(COMMAND ${FLTKCONFIG} --cc OUTPUT_VARIABLE
FLTK_C_COMPILER_QUERY_RESULT)
message(STATUS "Using C compiler (fltk-config -cc):
${FLTK_C_COMPILER_QUERY_RESULT}")
message(STATUS "Using C++ compiler (fltk-config --cxx):
${FLTK_CXX_COMPILER_QUERY_RESULT}")
find_program(CXX_COMPILER "${FLTK_CXX_COMPILER_QUERY_RESULT}")
find_program(C_COMPILER "${FLTK_C_COMPILER_QUERY_RESULT}")
message(STATUS "${CXX_COMPILER}")
message(STATUS "${C_COMPILER}")
set(CMAKE_CXX_COMPILER "${CXX_COMPILER}")
set(CMAKE_C_COMPILR "${C_COMPILER}")

And running this outputs:
-- Using C compiler (fltk-config -cc): gcc

-- Using C++ compiler (fltk-config --cxx): g++

-- CXX_COMPILER-NOTFOUND
-- C_COMPILER-NOTFOUND
-- /usr/local/bin/fltk-config
...

Thanks!
-deech
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20131021/97b2d1a4/attachment.htm>


More information about the CMake mailing list