The proper way of forcing a 32-bit compile using CMake

前端 未结 6 755
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 15:40

Sorry that there are many similar questions, but I do find that Googling for CMake queries always yields similar-but-not-the-same scenarios, conflicting CMake commands and s

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-07 16:24

    Even if this seems like extra works, I believe a proper solution is to use toolchain file in this case. Something like:

    # the name of the target operating system
    set(CMAKE_SYSTEM_NAME Linux)
    
    # which compilers to use for C and C++
    set(CMAKE_C_COMPILER gcc)
    set(CMAKE_C_FLAGS -m32)
    set(CMAKE_CXX_COMPILER g++)
    set(CMAKE_CXX_FLAGS -m32)
    
    # here is the target environment located
    set(CMAKE_FIND_ROOT_PATH   /usr/i486-linux-gnu )
    
    # adjust the default behaviour of the FIND_XXX() commands:
    # search headers and libraries in the target environment, search
    # programs in the host environment
    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
    

    Then usage is simply:

    $ cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake /path/to/source
    

    The important part here is that one is now able to specify a root dir path (CMAKE_FIND_ROOT_PATH) which should be used to search for third party lib. Indeed your compiler may not be smart enough to know where to search for an x86 Qt library on an x86_64 system.

    Having a toolchain file allows one to specify a different one on a par compiler basis, and you should be able to tweak the option when compiling in 32bits from a windows environement.

    Nowadays this is extra works since compiling 32bits from an x86_64 Linux OS is pretty much trivial, but this solution will work for other more exotic setup.

    For more information on toolchain file, one can check for example:

    • http://www.cmake.org/Wiki/CmakeMingw

提交回复
热议问题