To begin with, I'm new to CMake.
The project is for the Windows and Linux platform.
Folder Structure
root_project
|─── CMakeLists.txt
|─── Project 1
| |─── build
| | |─── Debug
| | └─── Release
| |─── source
| | |─── CMakeLists.txt
| | └─── include
| |─── resource
| └─── header
└─── Project 2
|─── build
| |─── Debug
| └─── Release
|─── source
| |─── CMakeLists.txt
| └─── include
|─── resource
└─── header
CMake files
The first of these files is for the root project and the second one is for "Project 1" and "Project 2" as just one line is different in the second file.
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.8.2)
# Project's name
project("root_project")
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
IF(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there. You may need to remove CMakeCache.txt.")
ENDIF()
OPTION(BUILD_TESTS "Decides whether the unit tests will be built." ON)
# C/C++ languages required.
enable_language(C)
enable_language(CXX)
# Set the C++ Version
message("!REQUIRED! -- Supported features = ${cxx_std_14}")
message("Supported features = ${cxx_std_17}")
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Only allow 64bit architecture
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
# 64bit
message(STATUS "Running on x86-64 platform. Proceeding...")
ELSEIF(CMAKE_SIZEOF_VOID_P EQUAL 4)
# 32bit
message(FATAL_ERROR "Running on x86 platform. This is not supported. Aborting...")
ELSE()
# unidentified architecture
message(FATAL_ERROR "Running on unidentified architecture. This is not supported. Aborting...")
ENDIF()
# Abort when OpenGL is not found
IF(NOT OPENGL_FOUND)
message(WARNING "Could not find OpenGL library.")
ENDIF()
IF(NOT VULKAN_FOUND)
message(WARNING "Could not find Vulkan library.")
ENDIF()
message(STATUS "----------------------------------------")
message(STATUS "CMake Binary Dir:" ${CMAKE_BINARY_DIR})
message(STATUS "CMake Source Dir:" ${CMAKE_SOURCE_DIR})
message(STATUS "CMake CFG Dir:" ${CMAKE_CFG_INTDIR})
message(STATUS "CMake exe Dir:" ${EXECUTABLE_OUTPUT_PATH})
message(STATUS "CMake lib Dir:" ${LIBRARY_OUTPUT_PATH})
# Add the modules
add_subdirectory("Project 1")
add subdirectory("Project 2")
The CMakeLists.txt for "Project 1" and "Project 2":
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.8.2)
project(Project 1)
# Set the version number of the project here
set(VERSION_MAJOR "0")
set(VERSION_MINOR "1")
set(VERSION_PATCH "0")
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
set(HEADERS
)
set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)
set(HEADERS_WIN32
)
set(SOURCES_WIN32
)
set(HEADERS_LINUX
)
set(SOURCES_LINUX
)
source_group(headers FILES ${HEADERS} ${HEADERS_WIN32} ${HEADERS_LINUX})
source_group(sources FILES ${SOURCES} ${SOURCES_WIN32} ${HEADERS_LINUX})
if(WIN32)
add_library(DarkEngine
${HEADERS}
${SOURCES}
${HEADERS_WIN32}
${SOURCES_WIN32}
)
ELSEIF(UNIX AND NOT APPLE)
add_library(DarkEngine
${HEADERS}
${SOURCES}
${HEADERS_LINUX}
${HEADERS_LINUX}
)
ELSE()
# The system is not supported
message(FATAL_ERROR "System not supported.")
ENDIF()
Note: "Project 1" is a library while "Project 2" is an executable and "Project 2" will be based on Project 1. Think of it as "Project 1" is an Engine and "Project 2" is the Game.
Question
Having this setup, from which folder do I have to call CMake from to be able to open the solution "root_project" in Visual Studio 2017 which contains both projects, "Project 1" and "Project 2". cmake .. -G "Visual Studio 15 2017 Win64"
How to put the binaries into the Debug folder when compiling in Debug mode and how to put them in the Release folder when compiling in Release mode? The Release and Debug folders are there to differentiate between Release and Debug builds like Visual Studio does.
I would try opening the root_project folder directly from the Developer Command Prompt, as per the instructions here:
https://blogs.msdn.microsoft.com/vcblog/2016/10/05/cmake-support-in-visual-studio/
cd root_project
devenv .
This built-in support dumps your binaries somewhere under a temp directory in %APPDATA%, so I added the following in my top-level CMakeLists.txt file to dump the binaries out to a specific folder:
# My understanding of what the output types mean. Check CMAKE documentation
# to confirm:
# ARCHIVE: Static & Import Library directory
# LIBRARY: DLL directory (MODULE)
# RUNTIME: DLL directory (SHARED)
# If you use these lines, VS will automatically create Debug and Release dirs
# under the directories you provide.
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../lib")
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../dll")
# In my project I want Debug and Release to clobber each other because
# I am building a DLL that I want to deploy next to an existing exe that
# loads it, so I explicitly point both scenarios to the same place
# (note the "_DEBUG" and "_RELEASE" suffixes to CMAKE_xxx_OUTPUT_DIRECTORY)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_LIST_DIR}/../lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_LIST_DIR}/../lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_LIST_DIR}/../dll")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_LIST_DIR}/../lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_LIST_DIR}/../lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_LIST_DIR}/../dll")
Related
I used CMake to compile a framework into three different .lib files, and one .sln file. The project is structured as such
UeiDaqFramework
|-CMakeLists.txt
|-build
| |-Framework_static.sln
| | - other cmake files and stuff
|-UeiDaqCore
| |-CMakeLists.txt
| |-source code for this subproject
|-UeiSimuDriver
| |-CMakeLists.txt
| |-source code for this subproject
|-UeiPDNADriver
| |-CMakeLists.txt
| |-source code for this subproject
|
|-Output
|-UeiDaqCore.lib
|-UeiSimuDriver.lib
|-UeiPDNADriver.lib
My test code require a Framework_static.lib in its include file instead of a .sln file, and I do not know how to combine the three existing .lib files using cmake. I did attempt to follow what this CMake - combine multiple libraries into one answer suggested, by just building all the source files into a single library, but I'm new to CMake, and it did not work. Here is the CmakeLists.txt file at the top directory of my project.
cmake_minimum_required(VERSION 3.13.0 FATAL_ERROR)
set(CMAKE_SYSTEM_VERSION CACHE TYPE INTERNAL FORCE)
project(Framework_static_vc15)
################################################################################
# Set target arch type if empty. Visual studio solution generator provides it.
################################################################################
if(NOT CMAKE_VS_PLATFORM_NAME)
set(CMAKE_VS_PLATFORM_NAME "x64")
endif()
message("${CMAKE_VS_PLATFORM_NAME} architecture in use")
if(NOT ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "Win32"))
message(FATAL_ERROR "${CMAKE_VS_PLATFORM_NAME} arch is not supported!")
endif()
################################################################################
# Global configuration types
################################################################################
set(CMAKE_CONFIGURATION_TYPES
"Debug"
"Release"
CACHE STRING "" FORCE
)
################################################################################
# Global compiler options
################################################################################
if(MSVC)
# remove default flags provided with CMake for MSVC
set(CMAKE_CXX_FLAGS "")
set(CMAKE_CXX_FLAGS_DEBUG "")
set(CMAKE_CXX_FLAGS_RELEASE "")
endif()
################################################################################
# Global linker options
################################################################################
if(MSVC)
# remove default flags provided with CMake for MSVC
set(CMAKE_EXE_LINKER_FLAGS "")
set(CMAKE_MODULE_LINKER_FLAGS "")
set(CMAKE_SHARED_LINKER_FLAGS "")
set(CMAKE_STATIC_LINKER_FLAGS "")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_STATIC_LINKER_FLAGS_DEBUG "${CMAKE_STATIC_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS}")
endif()
################################################################################
# Nuget packages function stub.
################################################################################
function(use_package TARGET PACKAGE VERSION)
message(WARNING "No implementation of use_package. Create yours.")
endfunction()
################################################################################
# Common utils
################################################################################
include(CMake/Utils.cmake)
################################################################################
# Additional Global Settings(add specific info there)
################################################################################
include(CMake/GlobalSettingsInclude.cmake OPTIONAL)
################################################################################
# Use solution folders feature
################################################################################
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
################################################################################
# Sub-projects
################################################################################
add_subdirectory(Source/UeiDaqCore)
add_subdirectory(Source/UeiPDNADriver)
add_subdirectory(Source/UeiSimuDriver)
These CMakeLists.txt files were generated automatically by a generator that takes a visual studio .sln file and converts it to a .txt tree. Heres the link to the converter if anyone is interested. https://cmakeconverter.readthedocs.io/en/develop/intro.html
Im not sure if I need to rewrite Just the top level .txt file, or if I need to rewrite all the CMakeLists.txt files. Any help would be appreciated.
I assume the build of the shared libs is within the subdirectories, because your CMakeLists.txt does not contain any add_library command.
To get a static library containing all sources you have to
Collect all sources
Add command to build the static library
For 1. add the following to every subdir/CMakeLists.txt
set(SOURCES # This creates a list of sources called SOURCES
myFileA.cpp # This adds an item to the list
myFileB.cpp
${SOURCES} # This adds all items of existing list SOURCES to the current list
PARENT_SCOPE # This makes SOURCES visible in the parent CMakeLists.txt
)
For 2. add the static library
add_library(myTestLib_static.c STATIC ${SOURCES})
To begin with, I'm new to CMake.
Folder structure
root_project
|─── CMakeLists.txt
|─── build
|─── Project 1
| |─── build
| | |─── Debug
| | └─── Release
| |─── CMakeLists.txt
| |─── source
| | └─── include
| |─── resource
| └─── header
└─── Project 2
|─── build
| |─── Debug
| └─── Release
|─── source
| |─── CMakeLists.txt
| └─── include
|─── resource
└─── header
CMake files
The CMakeLists.txt for the root_project:
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.8.2 FATAL_ERROR)
# Project's name
project("RootProject X")
IF(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there. You may need to remove CMakeCache.txt.")
ENDIF()
OPTION(BUILD_TESTS "Decides whether the unit tests will be built." ON)
# C/C++ languages required.
enable_language(C)
enable_language(CXX)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Only allow 64bit architecture
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
# 64bit
message(STATUS "Compiling for x86-64 platform. Proceeding...")
ELSEIF(CMAKE_SIZEOF_VOID_P EQUAL 4)
# 32bit
message(FATAL_ERROR "Compiling for x86 platform. This is not supported. Aborting...")
ELSE()
# unidentified architecture
message(FATAL_ERROR "Running on unidentified architecture. This is not supported. Aborting...")
ENDIF()
# Abort when OpenGL is not found
FIND_PACKAGE(OpenGL 4.5 REQUIRED)
IF(NOT VULKAN_FOUND)
message(WARNING "Could not find Vulkan library.")
ENDIF()
# Add the modules
add_subdirectory("Project 1")
add_subdirectory("Project 2")
The CMakeLists for Project 1:
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.8.2 FATAL_ERROR)
project("Project 1")
# Set the version number of the project here
set(VERSION_MAJOR "0")
set(VERSION_MINOR "1")
set(VERSION_PATCH "0")
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
find_package(OpenGL 4.5 REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/sources/third-party-include/)
link_libraries(${OPENGL_gl_LIBRARY})
add_definitions(${OpenGL_DEFINITIONS})
#include_directories(${CMAKE_CURRENT_SOURCE_DIR}/sources/include/)
# ToDo: Testing
set(HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/sources/include/Engine.h
)
set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/source/Engine.cpp
)
source_group(headers FILES ${HEADERS})
source_group(sources FILES ${SOURCES})
if(WIN32)
# currently add_executable for testing purpose
add_executable("Project 1" WIN32
#${HEADERS}
${SOURCES}
)
ELSEIF(UNIX AND NOT APPLE)
add_library("Project 1"
${HEADERS}
${SOURCES}
)
ELSE()
# The system is not supported
message(FATAL_ERROR "System not supported.")
ENDIF()
target_link_libraries(DarkEngine ${OPENGL_gl_LIBRARY})
The CMakeLists for Project 2:
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.8.2 FATAL_ERROR)
project("Project 2")
# Set the version number of the project here
set(VERSION_MAJOR "0")
set(VERSION_MINOR "1")
set(VERSION_PATCH "0")
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
set(HEADERS
)
set(SOURCES
)
source_group(headers FILES ${HEADERS})
source_group(sources FILES ${SOURCES})
add_executable("Project 2" WIN32
${HEADERS}
${SOURCES}
)
target_link_libraries("Project 2" "Project 1")
Question/Problem
Note: I am trying to achieve an out-of-source build where Project 1 is an Engine which is used from Project 2.
Commands I run under build of root_project: cmake .. -G "Visual Studio 15 2017 Win64"
Project 2 doesn't link against Project 1
Project 2 doesn't show up in VS 2017
My own header files under /include for Project 1 will be shown in External Dependencies in VS 2017 instead of source
Turning my comment into an answer
You just need to specify sources for your Project 2 target.
I've given you example a try and CMake gives you an configuration error (which is probably why it won't show up/doesn't rewrite the solution/project files, if you add the second project):
CMake Error at CMakeLists.txt:23 (add_executable):
add_executable called with incorrect number of arguments, no sources provided
And yes, if you add e.g. a not yet existing src/main.cpp to SOURCES you will get:
CMake Error at CMakeLists.txt:23 (add_executable):
Cannot find source file:
src/main.cpp
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx
So the workflow in CMake is always to first create the source file and then add to your CMakeLists.txt. With an IDE like VS you could just add a new item to your project (like you would do for non-generated projects) and then add it to your CMakeLists.txt file.
For some of my projects I've created a check_and_create_source() function called inside overwritten add_executable() calls, but using it for some years I can now say there is no real benefit in that approach. You still need to run a ZERO_CHECK build to get the empty files created/before you can start (and you get confused when you mistyped an inclusion for an existing source file).
And I personally don't like the globbing alternative (see Why is cmake file GLOB evil? and CMake/Ninja attempting to compile deleted `.cpp` file)
Well, I am new to CMake.
I have the following file structure
haze_removal
|---build
|---bin
| |--Test
| |--CMakeLists.txt
|---lib
| |--libtools.so
|---include
| |--tools.hpp
|---test
| |--main.cpp
| |--CMakeLists.txt
|---src
| |--tools.cpp
| |--CMakeLists.txt
|---CMakeLists.txt
The libtools.so is builded from ../src/tools.cpp. I build the whole project in ../build using the following cmake command:
cmake -DCMAKE_BUILD_TYPE=Debug ..
make
The Test is build from ../test/main.cpp
I build this project successfully. But when I debug Test using gdb ./Test, I can't skip in the function that from libtools.so.
These are my CMakeLists.txt from different directories.
CMakeLists.txt under haze_removal/
cmake_minimum_required(VERSION 2.8)
project(haze_removal)
# find needed package
find_package(OpenCV REQUIRED)
# library directory
add_subdirectory(src)
# test
add_subdirectory(test)
CMakeLists.txt under ../src/
# generate dynamic library
# add source file, include directories
aux_source_directory(. TOOLS_SRC)
include_directories(${PROJECT_SOURCE_DIR}/include)
# generate
add_library(tools SHARED ${TOOLS_SRC})
# set output directory and lib's name
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
set_target_properties(tools PROPERTIES OUTPUT_NAME "tools")
# link library
target_link_libraries(tools ${OpenCV_LIBS})
CMakeLists.txt under ../test/
# add source file, include directories, link directories
aux_source_directory(. EXE_SRC)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
# generate
add_executable(Test ${EXE_SRC})
# set output directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# link libraries
target_link_libraries(Test ${OpenCV_LIBS} libtools.so)
My question is how I can debug the functions that from libtools.
Well, I find that why I can't step in the dynamic library even though I set CMAKE_BUILD_TYPE=Debug. Because before I set the build model to Debug I used to set CMAKE_BUILD_TYPE=Release. After I change the build model, I didn't delete the files in the build directory.
I don't know whether the above explanation is right or not, but I do solve my problem.
Thx!
I have compiled program with cmake.The cmakelist.txt:
cmake_minimum_required (VERSION 2.6)
project (clustering)
IF(CMAKE_SIZEOF_VOID_P EQUAL 4)
SET(LIB_SUFFIX "")
ELSE(CMAKE_SIZEOF_VOID_P EQUAL 4)
SET(LIB_SUFFIX 64)
ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 4)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}")
find_package(Eigen2 REQUIRED)
include_directories(${Eigen2_INCLUDE_DIR})
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
FORCE)
endif(NOT CMAKE_BUILD_TYPE)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(LIB_NAME clusteringd)
else(CMAKE_BUILD_TYPE MATCHES "Debug")
set(LIB_NAME clustering)
endif(CMAKE_BUILD_TYPE MATCHES "Debug")
file(GLOB LIB_PUBLIC_HEADERS "${CMAKE_SOURCE_DIR}/*.h")
file(GLOB LIB_SOURCES "${CMAKE_SOURCE_DIR}/*.cpp")
#add_library (${LIB_NAME}-s STATIC ${LIB_PUBLIC_HEADERS} ${LIB_SOURCES})
add_library (${LIB_NAME} SHARED ${LIB_PUBLIC_HEADERS} ${LIB_SOURCES})
install(
TARGETS ${LIB_NAME}
LIBRARY DESTINATION lib${LIB_SUFFIX}
)
#install(
# TARGETS ${LIB_NAME}-s
# ARCHIVE DESTINATION lib${LIB_SUFFIX}
#)
install(
FILES ${LIB_PUBLIC_HEADERS}
DESTINATION include/${LIB_NAME}
)
The problem is that in my folder /clusteringmaster/build/CMakeFiles/2.8.12.2/ I have two folders CompilerIdC and CompilerIdCXX,both of them have exe file.As Sergey pointed out exe should be located in bin directory.When I list my CmakeFile
2.8.12.2 cmake.check_cache CMakeOutput.log Makefile2 progress.marks
clustering.dir CMakeDirectoryInformation.cmake CMakeTmp Makefile.cmake TargetDirectories.txt
From the authors tutorial
Build
You will need the Eigen2 library (libeigen2-devel) and CMake (only tested under Linux).
$ mkdir release
$ cd release
$ cmake ../
$ make
Should I write cmake -- build /home/milenko/clusteing-master/release?
I have not worked with cmake before,how should I solve this confusion?
One item is that should use the cmake variable ${PROJECT_NAME} as the target name for the add_library command. (Your project name is specified as "clustering" because of the project (clustering) statement.) To get a d suffix for the debug configuration build, use set(CMAKE_DEBUG_POSTFIX d). I also like to set the output directory variables using
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
(I will even set the _DEBUG and _RELEASE variants, but this may cause problems when you are first getting started, so I would wait before doing that.)
I've started a little project on GitHub for an OpenSceneGraph plugin. Here, I plan on learning how to setup projects with CMake, unit testing with Boost.Test and continuous integration, with Travis-CI.
It has this structure:
root
|-> cmake
|-> conf [Create the `library`-config.cmake and `library`-depends.cmake files]
|-> find [Find module for the custom `Boost` installation at work]
|-> examples
|-> include [*.h]
|-> src [*.src]
|-> test
|-> options [CMakeFile.txt and .cpp file for the test]
|
|-> CMakeFile.txt [Root file]
I have two machines: my laptop, with Linux Mint 16 and g++ 4.8, and my work computer, with Windows 8.1, the MinGW suite and g++ 4.5.
The project (development branch!) builds fine with make, but afterwards, when I try to run the tests with make test in Windows I always end getting these errors:
The program can't start because libboost_unit_test_framework-mgw45-mt-1_51.dll is missing
and in the terminal:
Running tests...
"C:\Program Files (x86)\CMake 2.8\bin\ctest.exe" --force-new-ctest-process
Test project D:/PROJECTS/osgGML/build
Start 1: testOptions
1/1 Test #1: testOptions ......................***Exception: Other 1.84 sec
0% tests passed, 1 tests failed out of 1
Total Test time (real) = 1.84 sec
The following tests FAILED:
1 - testOptions (OTHER_FAULT)
Errors while running CTest
mingw32-make: *** [test] Error 8
Whereas in Linux (tried in Linux Mint 16 and in the Travis-CI machine, it works).
The "root" CMakeFile:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(osgGML)
option(BUILD_SHARED_LIBS "Build shared libs" ON)
option(BUILD_EXAMPLES OFF)
option(BUILD_TESTS OFF)
option(BUILD_USE_LAMBDAS OFF)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Type of build" FORCE)
endif(NOT CMAKE_BUILD_TYPE)
set(LIBRARY_NAME "osgGML")
set(EXPORT_NAME "depends")
set(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "" FORCE)
set(CMAKE_COLOR_MAKEFILE ON)
mark_as_advanced(CMAKE_VERBOSE_MAKEFILE)
# Common locatoins for Unix and Mac OS X
set(DEFAULT_BIN_SUBDIR bin)
set(DEFAULT_LIB_SUBDIR lib)
set(DEFAULT_SHARE_SUBDIR share/cmake)
set(DEFAULT_INCLUDE_SUBDIR include)
set(OSGGML_BIN_SUBDIR ${DEFAULT_BIN_SUBDIR} CACHE STRING
"Subdirectory where executables will be installed")
set(OSGGML_LIB_SUBDIR ${DEFAULT_LIB_SUBDIR} CACHE STRING
"Subdirectory where libraries will be installed")
set(OSGGML_INCLUDE_SUBDIR ${DEFAULT_INCLUDE_SUBDIR} CACHE STRING
"Subdirectory where header files will be installed")
set(OSGGML_SHARE_SUBDIR ${DEFAULT_SHARE_SUBDIR} CACHE STRING
"Subdirectory where data will be installed")
# Full paths for the installation
set(OSGGML_BIN_DIR ${OSGGML_BIN_SUBDIR})
set(OSGGML_LIB_DIR ${OSGGML_LIB_SUBDIR})
set(OSGGML_INCLUDE_DIR ${OSGGML_INCLUDE_SUBDIR})
set(OSGGML_SHARE_DIR ${OSGGML_SHARE_SUBDIR})
##
# Set the build postfix extension according to what configuration is being built.
set(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "add a postfix, usually d")
set(CMAKE_RELEASE_POSTFIX "" CACHE STRING "add a postfix, usually empty")
set(CMAKE_RELWITHDEBINFO_POSTFIX "rd" CACHE STRING "empty")
set(CMAKE_MINSIZEREL_POSTFIX "s" CACHE STRING "add a postfix, empty")
if(CMAKE_BUILD_TYPE MATCHES "Release")
set(CMAKE_BUILD_POSTFIX "${CMAKE_RELEASE_POSTFIX}")
elseif(CMAKE_BUILD_TYPE MATCHES "MinSizeRel")
set(CMAKE_BUILD_POSTFIX "${CMAKE_MINSIZEREL_POSTFIX}")
elseif(CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo")
set(CMAKE_BUILD_POSTFIX "${CMAKE_RELWITHDEBINFO_POSTFIX}")
elseif(CMAKE_BUILD_TYPE MATCHES "Debug")
set(CMAKE_BUILD_POSTFIX "${CMAKE_DEBUG_POSTFIX}")
else()
set(CMAKE_BUILD_POSTFIX "")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
if(BUILD_USE_LAMBDAS)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
execute_process(
COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
if((GCC_VERSION VERSION_GREATER 4.7 OR GCC_VERSION VERSION_EQUAL 4.7))
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
endif()
endif()
endif()
find_package(OpenSceneGraph REQUIRED COMPONENTS
osg
osgDB
OpenThreads
)
include_directories(${OPENSCENEGRAPH_INCLUDE_DIRS})
link_directories(${OPENSCENEGRAPH_LIBRARY_DIRS})
# HACK!!!! DO THIS PROPERLY!!!
link_directories(${OPENSCENEGRAPH_BINARY_DIRS}/osgPlugins-3.2.1/)
set(DEPENDENCIES ${OPENSCENEGRAPH_LIBRARIES})
##
set(osgGML_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}/include")
set(osgGML_SOURCE_DIRS "${PROJECT_SOURCE_DIR}/src")
set(HEADERS ${osgGML_INCLUDE_DIRS}/DebugUtils.h
${osgGML_INCLUDE_DIRS}/GmlOptions.h
${osgGML_INCLUDE_DIRS}/GmlOptionsIO.h
${osgGML_INCLUDE_DIRS}/GraphVisitor.h
${osgGML_INCLUDE_DIRS}/ReaderWriterGML.h
${osgGML_INCLUDE_DIRS}/Setup.h
)
set(SOURCES ${osgGML_SOURCE_DIRS}/GmlOptions.cpp
${osgGML_SOURCE_DIRS}/GmlOptionsIO.cpp
${osgGML_SOURCE_DIRS}/GraphVisitor.cpp
${osgGML_SOURCE_DIRS}/ReaderWriterGML.cpp
)
include_directories(${osgGML_INCLUDE_DIRS})
add_library(${LIBRARY_NAME} ${SOURCES})
target_link_libraries(${LIBRARY_NAME} ${DEPENDENCIES})
#
install(TARGETS ${LIBRARY_NAME}
EXPORT ${EXPORT_NAME}
RUNTIME DESTINATION ${OSGGML_BIN_DIR}
LIBRARY DESTINATION ${OSGGML_LIB_DIR}
ARCHIVE DESTINATION ${OSGGML_LIB_DIR}
)
install(FILES ${HEADERS} DESTINATION ${OSGGML_INCLUDE_DIR})
if(BUILD_EXAMPLES)
message(STATUS "Building EXAMPLES.")
set(OSGGML_EXAMPLES_BIN_DIR share/examples)
add_subdirectory(examples/options)
add_subdirectory(examples/visitor)
endif()
if(BUILD_TESTS)
enable_testing()
message(STATUS "Building TESTS")
set(OSGGML_TESTS_BIN_DIR share/tests)
add_subdirectory(test/options)
endif()
###########
add_subdirectory(cmake/conf)
###########
The CMakeFile for the test:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(OptionsTesting)
set(CMAKE_BUILD_TYPE "RELEASE")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
link_directories(${CMAKE_BINARY_DIR}/lib)
include_directories(../../include)
set(DEPENDENCIES ${DEPENDENCIES} ${LIBRARY_NAME})
# Hack for my weird Boost installation in Windows.
if(WIN32 AND MINGW)
set(BOOST_ROOT "D:/PROYECTOS/SITEGI/LIBRERIAS/Boost")
include(../../cmake/find/find_boost.cmake)
else()
find_package(Boost REQUIRED COMPONENTS unit_test_framework)
endif()
include_directories (${Boost_INCLUDE_DIRS})
set(DEPENDENCIES ${DEPENDENCIES} ${Boost_LIBRARIES})
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(BOOST_ALL_DYN_LINK ON)
find_package(OpenSceneGraph REQUIRED COMPONENTS osg osgDB OpenThreads)
include_directories(${OPENSCENEGRAPH_INCLUDE_DIRS})
link_directories(${OPENSCENEGRAPH_LIBRARY_DIRS})
set(DEPENDENCIES ${DEPENDENCIES} ${OPENSCENEGRAPH_LIBRARIES})
file(GLOB TEST_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
set(TEST_DEST ${CMAKE_BINARY_DIR}/testBinaries)
set(TEST_LINK_DEPS ${TEST_LINK_DEPS} "-Wl,-rpath,${CMAKE_BINARY_DIR}")
set(TEST_LINK_DEPS ${TEST_LINK_DEPS} "-Wl,-rpath,${Boost_LIBRARY_DIRS}")
foreach(testSrc ${TEST_SRCS})
#Extract the filename without an extension (NAME_WE)
get_filename_component(testName ${testSrc} NAME_WE)
add_executable(${testName} ${testSrc})
target_link_libraries(${testName} ${DEPENDENCIES})
set_target_properties(${testName} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${TEST_DEST}
LINK_FLAGS "${TEST_LINK_DEPS}"
)
add_test(NAME ${testName}
WORKING_DIRECTORY ${TEST_DEST}
COMMAND ${TEST_DEST}/${testName}
)
endforeach(testSrc)
I'm trying to tell CMake to use the newly-generated osgGML library in the build directory when linking the test executable (I have checked, libosgGML.dll is in ${CMAKE_BINARY_DIR}.
In this version of the file, I added the rpath to the library. In a previous one, aside from -Wl,-rpath,<library_dirs> I also specified the -L<library_dirs> right after that.
I've tried to link directly the dll's too, but nothing worked.
How can I tell the test executable to "find" the libraries in their respective directories, instead of copying them to the executable dir?