• cmake编译(编译目标)x86或x64


    if(CMAKE_CL_64)    #CMAKE的内建变量,如果是true,就说明编译器的64位的,自然可以编译64bit的程序
    set(ADDRESS_MODEL 64)
    set(NODE_TARGET x64)
    else()
    set(ADDRESS_MODEL 32)
    set(NODE_TARGET x86)
    endif()

    看以上代码,判断下就行了

    CMAKE_CL_64内建的这个变量,官方文档给出来的解释是:

    Using the 64 bit compiler from Microsoft

    Set to true when using the 64 bit cl compiler from Microsoft.

    ###############update####################

     

    如果是GCC编译器或者其他的诸如sun Studio之类的,可以设置CFLAGS CXXFLAGS变量,加入-m32 或者-m64来编译x86或者x64的程序。

    如果是windows平台下VS的project文件的话,请使用VS generator,cmake -G "Visual Studio 10 Win64"就是编译为x64,cmake -G "Visual Studio 10 "就是编译为x86.

    如果是Mac OS X上,使用CMAKE_OSX_ARCHITECTURES这个CMAKE的内建变量。

    • cmake -DCMAKE_OSX_ARCHITECTURES=i386 /path/to/source/dir------编译为32位

      cmake -DCMAKE_OSX_ARCHITECTURES=x86_64 /path/to/source/dir------编译为64位

      cmake "-DCMAKE_OSX_ARCHITECTURES=x86_64;i386" /path/to/source/dir -------编译为通用8664架构和i386架构的二进制

     

    或者可以用另外一种方式,思路是这样的,其实编译为x86或者x64,本质上都是向linker传递特定的参数。如果是VS,或者是NMake的Makefiles的话,直接使用CMAKE内建的变量CMAKE_EXE_LINKER_FLAGS,来设置链接器的参数。VS toolchain 可以这样 set(CMAKE_EXE_LINKER_FLAGS "/machine:x86")。

    这个变量官方给出来的解释是:

    Linker flags to be used to create executables.

    These flags will be used by the linker when creating an executable.

    以下是构建一个console的demo:

     1 SET(PROJECT_NAME dls_test)
     2 
     3 PROJECT(${PROJECT_NAME})
     4 
     5 CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
     6 
     7 #ADD_DEFINITIONS(-DUNICODE -D_UNICODE)
     8 ADD_DEFINITIONS(-D_CONSOLE)
     9 
    10 
    11 SET(CMAKE_MFC_FLAG 1)  #0: Use Standard Windows Libraries            1: Use MFC in a Static Library        2: Use MFC in a Shared DLL
    12 
    13 
    14 include_directories(${PROJECT_SOURCE_DIR})
    15 include_directories(${PROJECT_SOURCE_DIR}/../inc)
    16 include_directories(${PROJECT_SOURCE_DIR}/../wlp_adapter)
    17 include_directories(${PROJECT_SOURCE_DIR}/../include)
    18 
    19 LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/../lib)
    20 #LINK_DIRECTORIES(${CMAKE_BINARY_DIR}/lib)
    21 
    22 file(GLOB SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}  main.cpp )
    23 source_group("src" FILES ${SRC})
    24 
    25 file(GLOB hdrs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.h)
    26 source_group("include" FILES ${hdrs})
    27 
    28 ADD_EXECUTABLE(${PROJECT_NAME}  ${SRC} ${hdrs})
    29 
    30 if(WIN32)
    31 if(CMAKE_BUILD_TYPE MATCHES "Debug")
    32   #set(OPENCV_LIB "opencv_core246d.lib" "opencv_highgui246d.lib" "opencv_imgproc246d.lib" IlmImfd.lib Comctl32.lib)
    33   TARGET_LINK_LIBRARIES(${PROJECT_NAME} wlp_adapter.lib hal.lib cbs.lib libglog_staticd.lib )
    34 else()
    35     TARGET_LINK_LIBRARIES(${PROJECT_NAME} wlp_adapter.lib hal.lib cbs.lib libglog_static.lib )
    36 endif()
    37 
    38 
    39 set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "/SUBSYSTEM:CONSOLE") 
    40 
    41 else()
    42 
    43    TARGET_LINK_LIBRARIES(${PROJECT_NAME} wlp_adapter hal cbs glog  pthread gomp rt glib-2.0 dl GL boost_system boost_filesystem boost_program_options boost_regex)
    44 
    45 endif()
    46 
    47 if(WIN32)
    48 else()
    49   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}   -std=c++0x  -fPIC ")
    50 endif()
    51   
    52 set(CompilerFlags
    53         CMAKE_CXX_FLAGS
    54         CMAKE_CXX_FLAGS_DEBUG
    55         CMAKE_CXX_FLAGS_RELEASE
    56         CMAKE_C_FLAGS
    57         CMAKE_C_FLAGS_DEBUG
    58         CMAKE_C_FLAGS_RELEASE
    59         )
    60 foreach(CompilerFlag ${CompilerFlags})
    61   string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
    62   string(REPLACE "/Ob0" "" ${CompilerFlag} "${${CompilerFlag}}")
    63   string(REPLACE "/TC" "" ${CompilerFlag} "${${CompilerFlag}}")
    64 endforeach()
    65 
    66 
    67 INSTALL(TARGETS ${prj_name}
    68     RUNTIME DESTINATION bin
    69     LIBRARY DESTINATION lib
    70     ARCHIVE DESTINATION lib
    71 )
    72 
    73 install(DIRECTORY ${PROJECT_SOURCE_DIR}
    74     DESTINATION "."
    75     )
    View Code

    reference:

    http://stackoverflow.com/questions/5334095/cmake-multiarchitecture-compilation

    http://blog.csdn.net/wh_xiexing/article/details/18301923

    http://www.cmake.org/cmake/help/v3.0/variable/CMAKE_CL_64.html

    http://www.cmake.org/cmake/help/v3.0/variable/CMAKE_EXE_LINKER_FLAGS.html

  • 相关阅读:
    动态规划记录 [动态更新]
    C++ STL map使用的注意事项记录
    20192425 2021202220192425 《网络与系统攻防技术》实验六实验报告
    20192425 202120222 《网络与系统攻防技术》实验五实验报告
    判断数据的增减
    还有多久退休
    按地名找所属市
    多表汇总
    优秀的编码实践
    client go 架构
  • 原文地址:https://www.cnblogs.com/foohack/p/3939506.html
Copyright © 2020-2023  润新知