Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cmake 编译32位和64位_Cmake - Fatal编程技术网

Cmake 编译32位和64位

Cmake 编译32位和64位,cmake,Cmake,我试图在同一个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 libr

我试图在同一个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)

我确信我遗漏了一些明显的东西,但看不出问题所在……

正如我在评论中所说,下面是我们如何做到这一点的一个例子

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}
)
即使在我们的生产过程中,它对我们也很有效

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

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

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

我们的软件也有同样的问题。我们选择的解决方案是创建一个选项来决定它是32还是64。在每种模式下,我们构建2倍于我们的软件。我可以提供一个例子来说明我们是如何做到这一点的。可能是重复的,但我不能解决的是如何在一个CMakeLists文件中构建32位和64位,包括静态库。我知道您可以检测系统的体系结构,但我很想将这两种体系结构都构建到发布目录的子文件夹中。如果您能够做到这一点,我不知道。但是您可以使用命令
install
将结果文件复制到所需的目录。我们也有解决办法。我将尝试举一个明确的例子。