Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.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
C++ 在windows上使用cmake for mingw对boost.test进行静态链接_C++_Boost_Cmake_Mingw_Static Linking - Fatal编程技术网

C++ 在windows上使用cmake for mingw对boost.test进行静态链接

C++ 在windows上使用cmake for mingw对boost.test进行静态链接,c++,boost,cmake,mingw,static-linking,C++,Boost,Cmake,Mingw,Static Linking,我想在Windows7上编译我的项目,使用mingw-4.9、cmake 3.8和Boost 1.63.0。 这是一个独立的单元测试项目。它接受生产源目录中的文件,测试文件夹中的测试文件实体模型(替换)也位于Separet文件夹中 我不确定我做错了什么 CMakeLists.txt cmake_minimum_required(VERSION 3.8) project(myProject) # C, C++ are enabled by default #enable_language(C

我想在Windows7上编译我的项目,使用mingw-4.9、cmake 3.8和Boost 1.63.0。 这是一个独立的单元测试项目。它接受生产源目录中的文件,测试文件夹中的测试文件实体模型(替换)也位于Separet文件夹中

我不确定我做错了什么

CMakeLists.txt

cmake_minimum_required(VERSION 3.8)

project(myProject)

# C, C++ are enabled by default
#enable_language(C CXX)

#definitions
add_definitions(-DWIN32)
add_definitions(-DBOOST_UNITTEST)
add_definitions(-DG_NEWIOSTREAM)

# Configure outputs 
set(ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/../../output/lib")
set(LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/../../output/lib")
set(RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/../../output/bin")

# Detect operating system
message(STATUS "Operating system is ${CMAKE_SYSTEM_NAME}")
if($(CMAKE_SYSTEM_NAME) STREQUAL "Linux")
    add_definitions(-DSYSTEM_LINUX)
endif()
if($(CMAKE_SYSTEM_NAME) STREQUAL "Windows")
    add_definitions(-DSYSTEM_WINDOWS)
endif()

# Detect host processor 
message (STATUS "The host processor is ${CMAKE_HOST_SYSTEM_PROCESSOR}")

# Settings the Boost Library 
set(Boost_USE_STATIC_LIBS ON) 
set(Boost_USE_MULTITHREADED ON)  
set(Boost_USE_STATIC_RUNTIME OFF) 

# Debuging for cmake
#set(Boost_DEBUG ON)

# Set path to the Boost Library
set(Boost_NO_SYSTEM_PATHS ON)
set(BOOST_ROOT "c:\\dev\\external")
set(BOOST_INCLUDEDIR "C:\\dev\\external\\include")
set(BOOST_LIBRARYDIR "C:\\dev\\external\\lib\\boost")
# At the end find the package, include and link
find_package(Boost 1.63.0 REQUIRED COMPONENTS unit_test_framework regex ) 
include_directories(${Boost_INCLUDE_DIRS})     

# Configure source files for production code
file(GLOB SOURCES ../../sources/*.cpp)

# Configure source files for unit-test code
file(GLOB SOURCES source/*.cpp)
file(GLOB SOURCES source/subs/*.cpp)

# Tell the cmake what needs to be builded
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_executable( myProject ${SOURCES} )

# Tell CMake to link it
target_link_libraries( myProject ${Boost_LIBRARIES} )
我的项目中的文件结构:

project
 |--sources    <-(production code)
 |--test
 |   |--source     <-(test code)
 |   |   |--subs    <-(substitutions, mockups)
 |   |--CMakeLists.txt 
 |--output
     |--test   <-(cmake generates here)
从CMake准备文件的目录,输出如下:

...
[ 87%] Building CXX object CMakeFiles/myProject.dir/source/subs/subs_trace_publ.cpp.obj
[100%] Linking CXX executable myProject.exe
C:/dev/external/lib/boost/libboost_unit_test_framework-mgw49-mt-1_63.a(unit_test_main.o):unit_test_main.cpp:(.text.startup+0x14):
undefined reference to `init_unit_test_suite(int, char**)'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\myProject.dir\build.make:260: recipe for target 'myProject.exe' failed
mingw32-make[2]: *** [myProject.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/myProject.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/myProject.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
同样的代码在CodeBlocks 16.01项目中对我有效。(Boost、MinGW、相同路径、设置、版本等)

如果需要,我可以提供更多信息


我也欢迎您提供一些关于CMakeLists.txt的提示。

过了一会儿,我自己找到了解决方案

除了文件(..)命令外,一切正常。 对于不同的位置,应使用不同的变量,例如:

# Configure source files for production code
file(GLOB SOURCES_PRODUCTION ../../sources/*.cpp)

# Configure source files for unit-test code
file(GLOB SOURCES_TEST source/*.cpp)
file(GLOB SOURCES_SUBS source/subs/*.cpp)
然后:

add_executable( myProject ${SOURCES_PRODUCTION} ${SOURCES_TEST} ${SOURCES_SUBS})
如果使用问题中的变量源,则每个分配都会覆盖数据,最后只包含最后一条路径

add_executable( myProject ${SOURCES_PRODUCTION} ${SOURCES_TEST} ${SOURCES_SUBS})