I'm trying to compile some code for 32 and 64 bit in the same CMakeLists.txt file. I thought the easiest way to do it would be to use a function. The (static) libraries used in the compilation are also built in the CMakeLists.txt file. However, despite building them in different directories, CMake complains that:
add_library cannot create target "mylib" because another target with
the same name already exists. The existing target is a static library
created in source directory "/home/chris/proj".
with the problem code being:
cmake_minimum_required (VERSION 2.6 FATAL_ERROR)
enable_language(Fortran)
project(myproj)
set(libfolder ${PROJECT_SOURCE_DIR}/lib/)
function(build bit)
message("Build library")
set(BUILD_BINARY_DIR ${PROJECT_BINARY_DIR}/rel-${bit})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${BUILD_BINARY_DIR}/bin)
add_library(mylib STATIC ${libfolder}/mylib.for)
set(CMAKE_Fortran_FLAGS "-m${bit}")
endfunction()
build(32)
build(64)
I'm sure I'm missing something obvious, but can't see the problem...