[CMake] Variables not persisting with multiple projects

Collin Eggert ahelper2 at gmail.com
Wed Aug 1 02:53:20 EDT 2012


Hello,

I am working with cross-compiling code using CMake.  I am able to get
the following CMakeLists.txt to work:

# ----------
SET(CMAKE_TOOLCHAIN_FILE ../toolchain/Prizm.cmake)

project(example1)
file(GLOB EXAMPLE1_SRC "src/example1/*.c*")
add_executable(example1 ${EXAMPLE1_SRC})
# ----------

This works just fine, everything is fine when I watch the output of
`make VERBOSE=1`.  Now, I need to compile multiple targets.  I tried
the following:

# ----------
SET(CMAKE_TOOLCHAIN_FILE ../toolchain/Prizm.cmake)

project(example1)
project(example2)

file(GLOB EXAMPLE1_SRC "src/example1/*.c*")
file(GLOB EXAMPLE2_SRC "src/example2/*.c*")

add_executable(example1 ${EXAMPLE1_SRC})
add_executable(example2 ${EXAMPLE2_SRC})
# ----------

However, when I look at the readout from `make VERBOSE=1`, I notice
that for one project, CMAKE_C_FLAGS/CMAKE_CXX_FLAGS/etc. are all set
and are used when compiling.  When it gets the the other project,
these variables are reset to being empty.  Below is the
toolchain/Prizm.cmake file:

# ----------
INCLUDE(CMakeForceCompiler)

SET(CMAKE_MODULE_PATH ../)
SET(CMAKE_SYSTEM_NAME Prizm)
SET(CMAKE_C_COMPILER sh3eb-elf-gcc)
SET(CMAKE_CXX_COMPILER sh3eb-elf-g++)

SET(CMAKE_FIND_ROOT_PATH ../)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

# CMake tries to test using libc linked in, but libc isn't available,
so forcing the go-ahead here
SET(CMAKE_C_COMPILER_WORKS 1)
SET(CMAKE_CXX_COMPILER_WORKS 1)
SET(CMAKE_EXECUTABLE_SUFFIX ".bin")
# ----------

And below is my Platform/Prizm.cmake file:

# ----------
SET(CMAKE_EXECUTABLE_SUFFIX ".bin")
SET(CMAKE_C_OUTPUT_EXTENSION ".o")
SET(CMAKE_CXX_OUTPUT_EXTENSION ".o")
SET(CMAKE_C_FLAGS "-Os -Wall -mb -m4a-nofpu -mhitachi -nostdlib
-ffunction-sections -fdata-sections
-I${CMAKE_CURRENT_SOURCE_DIR}/../include")
SET(CMAKE_CXX_FLAGS "-Os -Wall -mb -m4a-nofpu -mhitachi -nostdlib
-ffunction-sections -fdata-sections
-I${CMAKE_CURRENT_SOURCE_DIR}/../include")

LINK_LIBRARIES(c)
# ----------

I am compiling using:

$ cmake .
<omitted>
$ make VERBOSE=1
<... example1 ...>
/usr/local/cross/bin/sh3eb-elf-gcc   -Os -Wall -mb -m4a-nofpu
-mhitachi -nostdlib -ffunction-sections -fdata-sections
-I/home/HAL/projects/libfxcg/example/../include   -o
CMakeFiles/example1.dir/src/example1/example.o   -c
/home/HAL/projects/libfxcg/example/src/example1/example.c
<... linking ...>
/usr/local/cross/bin/sh3eb-elf-gcc  -Os -Wall -mb -m4a-nofpu -mhitachi
-nostdlib -ffunction-sections -fdata-sections
-I/home/HAL/projects/libfxcg/example/../include  -L../lib
-T../toolchain/prizm.x -Wl,-static -Wl,-gc-sections -nostdlib
CMakeFiles/example1.dir/src/example1/example.o  -o example1.bin  -lc
-lc -lc
<... example2 ...>
/usr/local/cross/bin/sh3eb-elf-g++     -o
CMakeFiles/example2.dir/src/example2/main.o -c
/home/HAL/projects/libfxcg/example/src/example2/main.cpp
<... errors out from compile errors ...<

I have been reading whatever information I could find for
cross-compiling, but I never came across this problem. I am currently
running cmake version 2.8.8.

Any help at all would be greatly appreciated!
AHelper


More information about the CMake mailing list