[CMake] Compiling 32- *and* 64-bit in same CMakeLists.txt file

Matthew Woehlke matthew.woehlke at kitware.com
Wed Jun 26 11:32:08 EDT 2013


On 2013-06-26 07:12, Chris Wood wrote:
> 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:
>
>      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...

Don't give targets the same names?

There is a CMake option/policy somewhere to allow it, but it's fragile 
and will break with some generators (e.g. Ninja, which happens to be 
probably the best supported build tool you could use). Trying to have 
multiple targets with the same name is troublesome and is best avoided.

I would recommend adding ${bit} to the target name. You can use the 
OUTPUT_NAME property if you absolutely need the file names of the 
resulting libraries to be the same.

-- 
Matthew



More information about the CMake mailing list