简体   繁体   中英

cmake and 2 libraries

I have a library in my project that I would like to compile once in 32 bits and than on 64 bits (at the same time, eg at the end of my compilation I would like to have the 2 versions). Is that possible or I have to call 2 times cmake + make but with different parameters?

Essentially the answer depends on the toolchain available. For example the older SJLJ MinGW gcc versions are multi-target and compiling 32-bit with them can be done by adding a command line option -m32 - this can easily be done by CMake.

However, in a single target gcc toolchain (which the majority are these days) you can only target one architecture. That means you have to get CMake to use two toolchains, one for each target architecture you're trying to support.

You're best off configuring and building for both 32-bit and 64-bit targets.

Example

A quick example if you do want to use a multi-target toolchain with CMake. This example uses the MinGW SJLJ toolchain

CMakeLists.txt

cmake_minimum_required( VERSION 2.8 )

project( mylib )

add_library( mylib_32 mylib.c mylib.h )
set_target_properties( mylib_32 PROPERTIES COMPILE_FLAGS "${CMAKE_C_FLAGS} -m32" )
set_target_properties( mylib_32 PROPERTIES LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32" )

add_executable( test_32 test.c )
target_link_libraries( test_32 mylib_32 )
set_target_properties( test_32 PROPERTIES COMPILE_FLAGS "${CMAKE_C_FLAGS} -m32" )
set_target_properties( test_32 PROPERTIES LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32" )

add_library( mylib_64 mylib.c mylib.h )
add_executable( test_64 test.c )
target_link_libraries( test_64 mylib_64 )

mylib.c

int PointerSize( void )
{
    return sizeof( void* );
}

mylib.h

#ifndef MYLIB_H
#define MYLIB_H

extern int PointerSize( void );

#endif

test.c

#include <stdio.h>

#include "mylib.h"

int main( int argc, char* argv[] )
{
    printf( "Pointer Size: %d\n", PointerSize() );
    return 0;
}

Output

Running the two resulting programs gives a 32-bit and 64-bit result for the library function PointerSize()

C:\>test_32.exe
Pointer Size: 4

C:\>test_64.exe
Pointer Size: 8

My simple answer is NO . 64 and 32-bit builds should be considered separate platforms; nothing about the generated artifacts are compatible. (This is not a commentary on cmake but rather how the operating systems, at least linux and windows, are implemented.) From a single cmake build, you cannot build for multiple platforms. There are generators that support multiple configurations (ie, Debug and Release), but none that support multiple platforms.

With that being said, using out-of-source cmake builds you can generate builds for multiple platforms using something like:

mkdir build-x64
mkdir build-x86

cd build-x64
cmake -G"Visual Studio 10 2010 Win64" ..
cmake --build .

cd ../build-x86
cmake -G"Visual Studio 10 2010" ..
cmake --build .

This may not be quite as simple as you were hoping, but I do not think it is that bad. It is certainly a way I have been able to use cmake and support both 32-bit and 64-bit builds.

thank you for your attention. As Brian said, it depends on the toolchain. His example is a good one! I had the same idea and it seems to work. I used add_library() and for each library I defined a target properties. Implementing it in this way, my cmake + make generates my library in 32 and 64 bits and I do not need to run to times the cmake + make. In the context of my project I need to create only one executable file in 32 bits but some libraries are required in 64 bit for other usage (common libraries between projects:) ).

Anton

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM