簡體   English   中英

使用MingW在Windows上為c ++ 11代碼適應cmake構建系統

[英]Adapting cmake build system for c++11 code on Windows with MingW

我一直在使用可以在OSX上很好地構建的開源庫,並且我正在嘗試在Windows上構建它。 開發人員本人幾乎沒有Windows經驗,並且知道其他人過去曾經做過,但是不確定如何做。

我遇到的障礙是如何告訴MingW使用C ++ 11。

CMakeLists.txt(來自此倉庫 ):

cmake_minimum_required(VERSION 2.8)

set(ENTITYX_MAJOR_VERSION 0)
set(ENTITYX_MINOR_VERSION 8)
set(ENTITYX_PATCH_VERSION 1)
set(ENTITYX_VERSION ${ENTITYX_MAJOR_VERSION}.${ENTITYX_MINOR_VERSION}.${ENTITYX_PATCH_VERSION})


include_directories(${CMAKE_CURRENT_LIST_DIR})

set(ENTITYX_BUILD_TESTING false CACHE BOOL "Enable building of tests.")
set(ENTITYX_RUN_BENCHMARKS false CACHE BOOL "Run benchmarks (in conjunction with -DENTITYX_BUILD_TESTING=1).")
set(ENTITYX_MAX_COMPONENTS 64 CACHE STRING "Set the maximum number of components.")
set(ENTITYX_USE_CPP11_STDLIB true CACHE BOOL "For Clang, specify whether to use libc++ (-stdlib=libc++).")
set(ENTITYX_BUILD_SHARED false CACHE BOOL "Build shared libraries?")

include(${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
include(CheckCXXSourceCompiles)

# Default compiler args
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Werror -Wall -Wextra -Wno-unused-parameter -Wno-error=unused-variable -Wno-error=sign-compare -std=c++11")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")

# C++11 feature checks
include(CheckCXX11Features.cmake)

add_definitions(-DGTEST_USE_OWN_TR1_TUPLE=1)
set(OLD_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
check_cxx_source_compiles(
"
#include <memory>

int main() {
    std::shared_ptr<int>();
}
"
ENTITYX_HAVE_CXX11_STDLIB
)

if (NOT ENTITYX_HAVE_CXX11_STDLIB)
    message("-- Not using -stdlib=libc++ (test failed to build)")
    set(CMAKE_CXX_FLAGS "${OLD_CMAKE_CXX_FLAGS}")
else ()
    message("-- Using -stdlib=libc++")
endif ()


# Misc features
check_include_file("stdint.h" HAVE_STDINT_H)

macro(require FEATURE_NAME MESSAGE_STRING)
    if (NOT ${${FEATURE_NAME}})
        message(FATAL_ERROR "${MESSAGE_STRING} required -- ${${FEATURE_NAME}}")
    else()
        message("--   ${MESSAGE_STRING} found")
    endif()
endmacro(require)

macro(create_test TARGET_NAME SOURCE)
    add_executable(${TARGET_NAME} ${SOURCE})
    target_link_libraries(
        ${TARGET_NAME}
        entityx
        gtest
        gtest_main
        ${ARGN}
        )
    add_test(${TARGET_NAME} ${TARGET_NAME})
endmacro()

if (NOT CMAKE_BUILD_TYPE)
    message("-- Defaulting to release build (use -DCMAKE_BUILD_TYPE:STRING=Debug for debug build)")
    set(CMAKE_BUILD_TYPE "Release")
endif()

message("-- Checking C++ features")
require(HAS_CXX11_AUTO "C++11 auto support")
require(HAS_CXX11_NULLPTR "C++11 nullptr support")
require(HAS_CXX11_RVALUE_REFERENCES "C++11 rvalue reference support")
#require(HAS_CXX11_CSTDINT_H "C++11 stdint support")
require(HAS_CXX11_VARIADIC_TEMPLATES "C++11 variadic templates")
require(HAS_CXX11_RVALUE_REFERENCES "C++11 rvalue references")
require(HAS_CXX11_LONG_LONG "C++11 long long")
require(HAS_CXX11_LONG_LONG "C++11 lambdas")

message("-- Checking misc features")
require(HAVE_STDINT_H "stdint.h")

set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")

# Things to install
set(install_libs entityx)

set(sources entityx/tags/TagsComponent.cc entityx/deps/Dependencies.cc entityx/System.cc entityx/Event.cc entityx/Entity.cc entityx/Manager.cc entityx/help/Timer.cc)
add_library(entityx STATIC ${sources})

if (ENTITYX_BUILD_SHARED)
    message("-- Building shared libraries (-DENTITYX_BUILD_SHARED=0 to only build static librarires)")
    add_library(entityx_shared SHARED ${sources})
    target_link_libraries(
        entityx_shared
        )
    set_target_properties(entityx_shared PROPERTIES OUTPUT_NAME entityx)
    list(APPEND install_libs entityx_shared)
endif (ENTITYX_BUILD_SHARED)

if (ENTITYX_BUILD_TESTING)
    #find_package(Boost 1.48.0 REQUIRED COMPONENTS timer system)
    add_subdirectory(gtest-1.6.0)
    include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
    enable_testing()
    create_test(entity_test entityx/Entity_test.cc)
    create_test(event_test entityx/Event_test.cc)
    create_test(system_test entityx/System_test.cc)
    create_test(tags_component_test entityx/tags/TagsComponent_test.cc)
    create_test(dependencies_test entityx/deps/Dependencies_test.cc)
    if (ENTITYX_RUN_BENCHMARKS)
        message("-- Running benchmarks")
        add_definitions(-DGTEST_USE_OWN_TR1_TUPLE=1 -DBOOST_NO_CXX11_NUMERIC_LIMITS=1)
        create_test(benchmarks_test entityx/Benchmarks_test.cc)
    else ()
        message("-- Not running benchmarks (use -DENTITYX_RUN_BENCHMARKS=1 to enable)")
    endif ()
endif (ENTITYX_BUILD_TESTING)


configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/entityx/config.h.in
    ${CMAKE_CURRENT_SOURCE_DIR}/entityx/config.h
)

install(
    DIRECTORY "entityx"
    DESTINATION "include"
    FILES_MATCHING PATTERN "*.h"
    )

install(
    TARGETS ${install_libs}
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    )

這是構建嘗試的輸出:

PS C:\Code\entityx\build> cmake -G "MinGW Makefiles" -DENTITYX_BUILD_SHARED=1 -DENTITYX_BUILD_TESTING=1 -DENTITYX_BUILD_PYTHON=0 ..
-- The C compiler identification is GNU 4.8.1
-- The CXX compiler identification is GNU 4.8.1
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Checking C++11 support for "auto" (N2546)
-- Checking C++11 support for "auto" (N2546) -- works
-- Checking C++11 support for "nullptr" (N2431)
-- Checking C++11 support for "nullptr" (N2431) -- works
-- Checking C++11 support for "lambda" (N2927)
-- Checking C++11 support for "lambda" (N2927) -- works
-- Checking C++11 support for "static_assert" (N1720)
-- Checking C++11 support for "static_assert" (N1720) -- works
-- Checking C++11 support for "rvalue_references" (N2118)
-- Checking C++11 support for "rvalue_references" (N2118) -- works
-- Checking C++11 support for "decltype" (N2343)
-- Checking C++11 support for "decltype" (N2343) -- works
-- Checking C++11 support for "cstdint"
-- Checking C++11 support for "cstdint" -- works
-- Checking C++11 support for "long_long" (N1811)
-- Checking C++11 support for "long_long" (N1811) -- works
-- Checking C++11 support for "variadic_templates" (N2555)
-- Checking C++11 support for "variadic_templates" (N2555) -- works
-- Checking C++11 support for "constexpr" (N2235)
-- Checking C++11 support for "constexpr" (N2235) -- works
-- Checking C++11 support for "sizeof_member" (N2253)
-- Checking C++11 support for "sizeof_member" (N2253) -- works
-- Checking C++11 support for "__func__" (N2340)
-- Checking C++11 support for "__func__" (N2340) -- works
-- Performing Test ENTITYX_HAVE_CXX11_STDLIB
-- Performing Test ENTITYX_HAVE_CXX11_STDLIB - Failed
-- Not using -stdlib=libc++ (test failed to build)
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Defaulting to release build (use -DCMAKE_BUILD_TYPE:STRING=Debug for debug build)
-- Checking C++ features
--   C++11 auto support found
--   C++11 nullptr support found
--   C++11 rvalue reference support found
--   C++11 variadic templates found
--   C++11 rvalue references found
--   C++11 long long found
--   C++11 lambdas found
-- Checking misc features
--   stdint.h found
-- Python support disabled
-- Building shared libraries (-DENTITYX_BUILD_SHARED=0 to only build static libraries)
-- Found PythonInterp: G:/Python27/python.exe (found version "2.7.5")
-- Looking for include file pthread.h
-- Looking for include file pthread.h - not found
-- Found Threads: TRUE
-- Not running benchmarks (use -DENTITYX_RUN_BENCHMARKS=1 to enable)
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Code/entityx/build

這是CMakeError.log

Performing C++ SOURCE FILE Test ENTITYX_HAVE_CXX11_STDLIB failed with the following output:
Change Dir: C:/Code/entityx/build/CMakeFiles/CMakeTmp

Run Build Command:C:/MinGW/bin/mingw32-make.exe "cmTryCompileExec424246307/fast"
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\cmTryCompileExec424246307.dir\build.make CMakeFiles/cmTryCompileExec424246307.dir/build

mingw32-make.exe[1]: Entering directory 'C:/Code/entityx/build/CMakeFiles/CMakeTmp'

"G:\Program Files (x86)\CMake 2.8\bin\cmake.exe" -E cmake_progress_report C:\Code\entityx\build\CMakeFiles\CMakeTmp\CMakeFiles 1

Building CXX object CMakeFiles/cmTryCompileExec424246307.dir/src.cxx.obj

C:\MinGW\bin\g++.exe    -pedantic -Werror -Wall -Wextra -Wno-unused-parameter -Wno-error=unused-variable -Wno-error=sign-compare -std=c++11 -std=gnu++11 -stdlib=libc++ -DENTITYX_HAVE_CXX11_STDLIB   -o CMakeFiles\cmTryCompileExec424246307.dir\src.cxx.obj -c C:\Code\entityx\build\CMakeFiles\CMakeTmp\src.cxx

g++.exe: error: unrecognized command line option '-stdlib=libc++'

CMakeFiles\cmTryCompileExec424246307.dir\build.make:59: recipe for target 'CMakeFiles/cmTryCompileExec424246307.dir/src.cxx.obj' failed

mingw32-make.exe[1]: *** [CMakeFiles/cmTryCompileExec424246307.dir/src.cxx.obj] Error 1

mingw32-make.exe[1]: Leaving directory 'C:/Code/entityx/build/CMakeFiles/CMakeTmp'

Makefile:116: recipe for target 'cmTryCompileExec424246307/fast' failed

mingw32-make.exe: *** [cmTryCompileExec424246307/fast] Error 2


Source file was:

#include <memory>

int main() {
    std::shared_ptr<int>();
}

因此,問題出現了,當我在OSX上運行cmake時,-stdlib = libc ++是有效的選項,而在Windows上的MingW下卻不是。 我該如何解決? 請注意,在OSX上,我正在使用Clang 5.0.0進行構建

注意:我知道以后我將不得不獲得pthread.h Windows版本,但是我想首先解決更大的問題。

這是因為libc ++是Clang的常規標准C ++庫,這是OSX上的默認庫,但是MinGW使用GCC擁有自己的標准庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM