【问题标题】:Compiling 32 and 64 bit编译 32 位和 64 位
【发布时间】:2013-06-26 10:15:22
【问题描述】:

我正在尝试在同一个 CMakeLists.txt 文件中为 32 位和 64 位编译一些代码。我认为最简单的方法是使用函数。编译中使用的(静态)库也构建在 CMakeLists.txt 文件中。然而,尽管将它们构建在不同的目录中,CMake 抱怨说:

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".

问题代码是:

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)

我确定我遗漏了一些明显的东西,但看不到问题...

【问题讨论】:

  • 我们的软件遇到了同样的问题。我们选择了这个解决方案来创建一个选项来决定它是 32 还是 64。我们在每种模式下构建了 2 倍的软件。我可以为您提供我们如何做到这一点的示例。

标签: cmake


【解决方案1】:

正如我在评论中所说,这是我们如何做到这一点的示例。

if( CMAKE_SIZEOF_VOID_P EQUAL 8 )
    MESSAGE( "64 bits compiler detected" )
    SET( EX_PLATFORM 64 )
    SET( EX_PLATFORM_NAME "x64" )
else( CMAKE_SIZEOF_VOID_P EQUAL 8 ) 
    MESSAGE( "32 bits compiler detected" )
    SET( EX_PLATFORM 32 )
    SET( EX_PLATFORM_NAME "x86" )
endif( CMAKE_SIZEOF_VOID_P EQUAL 8 )

... 

IF( EX_PLATFORM EQUAL 64 )
MESSAGE( "Outputting to lib64 and bin64" )

# ---------- Setup output Directories -------------------------
SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY
   ${YourSoftwarePath}/lib64
   CACHE PATH
   "Single Directory for all Libraries"
   )

# --------- Setup the Executable output Directory -------------
SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY
   ${YourSoftwarePath}/bin64
   CACHE PATH
   "Single Directory for all Executables."
   )

# --------- Setup the Executable output Directory -------------
SET (CMAKE_ARCHIVE_OUTPUT_DIRECTORY
   ${YourSoftwarePath}/lib64
   CACHE PATH
   "Single Directory for all static libraries."
   )
ELSE( EX_PLATFORM EQUAL 64 )
# ---------- Setup output Directories -------------------------
SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY
   ${YourSoftwarePath}/lib
   CACHE PATH
   "Single Directory for all Libraries"
   )

# --------- Setup the Executable output Directory -------------
SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY
   ${YourSoftwarePath}/bin
   CACHE PATH
   "Single Directory for all Executables."
   )

# --------- Setup the Executable output Directory -------------
SET (CMAKE_ARCHIVE_OUTPUT_DIRECTORY
   ${YourSoftwarePath}/lib
   CACHE PATH
   "Single Directory for all static libraries."
   )
ENDIF( EX_PLATFORM EQUAL 64 )

...


add_library(YourSoftware SHARED
    ${INCLUDES}
    ${SRC}
)

它对我们来说效果很好,即使在我们的生产过程中也是如此。

它允许我们为 32 位和 64 位准备好我们的配置。之后,我们必须在这两个平台上进行构建。

【讨论】:

  • 嗯,我无法解决的是如何在 1 个 CMakeLists 文件中构建 32 位和 64 位,包括静态库。我知道你可以检测系统的架构,但我很想将这两种架构都构建到发布目录的子文件夹中
  • 如果你能做到,我不会。但是您可以使用命令install 将结果文件复制到所需的目录。我们也有一个解决方法。我会尝试做一个明确的例子。
【解决方案2】:

add_library 中指定的<name> 对应于逻辑目标名称,并且在项目中必须全局唯一。因此,您定义相同的目标 (mylib) 两次,这是被禁止的。 但是,您可以定义两个不同的目标并指定目标的输出名称以再次生成相同的库名称:

add_library(mylib${bit} STATIC ${libfolder}/mylib.for)
set_target_properties(mylib${bit} PROPERTIES OUTPUT_NAME mylib)

http://www.cmake.org/cmake/help/v3.0/command/add_library.html http://www.cmake.org/cmake/help/v3.0/command/set_target_properties.html

【讨论】:

    猜你喜欢
    • 2011-04-12
    • 2023-03-03
    • 2014-01-12
    • 2018-08-04
    • 1970-01-01
    • 2017-03-11
    • 2017-01-21
    • 2013-08-25
    • 1970-01-01
    相关资源
    最近更新 更多