How to create dependencies in CMake - c++

I'm trying to improve my project layout.
Here's (some of) my CMakeLists.txt files:
project(Numerical CXX)
include(cotire)
cmake_minimum_required(VERSION 3.1)
set (CMAKE_CXX_STANDARD 14)
add_executable(hw1 hw1.cpp
linalg/lu.cpp
linalg/banded.cpp
)
add_executable(hw2 hw2.cpp
linalg/cholesky.cpp
linalg/lu.cpp
linalg/banded.cpp
)
add_executable(hw3 hw3.cpp
linalg/solvers-new.cpp
linalg/cholesky.cpp
linalg/lu.cpp
linalg/banded.cpp
)
...
lu.h includes banded.h, so anything that needs lu will also need banded. This requires redundancy in my project as seen above. Is there a way that I don't have to add banded.cpp every time I add lu.cpp?

#Amadeus's answer works, but I think a better answer is to take the common files and move them into a library:
project(Numerical CXX)
include(cotire)
cmake_minimum_required(VERSION 3.1)
set (CMAKE_CXX_STANDARD 14)
add_library(CommonLib STATIC
linalg/lu.cpp
linalg/banded.cpp
)
add_executable(hw1
hw1.cpp
)
target_link_libraries(hw1 LINK_PUBLIC
CommonLib
)
add_executable(hw2
hw2.cpp
linalg/cholesky.cpp
)
target_link_libraries(hw2 LINK_PUBLIC
CommonLib
)
add_executable(hw3
hw3.cpp
linalg/solvers-new.cpp
linalg/cholesky.cpp
)
target_link_libraries(hw3 LINK_PUBLIC
CommonLib
)

Related

Issues using Protocol Buffers in a C++ Maya API Plugin

Long story short, I would like to create a Protocol Buffer object inside the initializePlugin(), defined in pluginMain.cpp as shown below:
// pluginMain.cpp
#include "hello.pb.h"
MStatus initializePlugin( MObject obj )
{
MGlobal::displayInfo( "Plugin is initialized!" );
hellopb_plugin::Foo f1;
return MStatus::kSuccess;
}
MStatus uninitializePlugin( MObject obj )
{
...
}
hellopb_plugin is a very simple protobuf type defined by the following hello.proto file:
// hello.proto
syntax = "proto3";
package hellopb_plugin;
message Foo
{
optional string name = 1;
}
The CMakeLists.txt I use includes the following:
cmake_minimum_required(VERSION 3.12)
if (NOT DEFINED CMAKE_TOOLCHAIN_FILE AND DEFINED ENV{CMAKE_TOOLCHAIN_FILE})
set(CMAKE_TOOLCHAIN_FILE $ENV{CMAKE_TOOLCHAIN_FILE})
endif()
project( Hello )
find_package( Protobuf REQUIRED )
include_directories( ${Protobuf_INCLUDE_DIRS} )
file( TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR} PROTO_INPUT_PATH )
file( TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR} PROTO_OUTPUT_PATH )
file( GLOB PROTO_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.proto" )
foreach( proto ${PROTO_FILES})
file( TO_NATIVE_PATH ${proto} proto_native )
execute_process( COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --proto_path=${PROTO_INPUT_PATH} --cpp_out=${PROTO_OUTPUT_PATH} ${proto_native} )
endforeach()
file( GLOB HDRS "${CMAKE_CURRENT_SOURCE_DIR}/*.pb.h" )
file( GLOB SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.pb.cc" )
add_library( ${PROJECT_NAME}Proto ${HDRS} ${SRCS} )
link_libraries( ${PROJECT_NAME}Proto ${Protobuf_LIBRARIES} )
set( ${PROJECT_NAME}Proto_PATH ${PROJECT_NAME}Proto )
set( SOURCE_FILES pluginMain.cpp )
set( LIBRARIES OpenMaya OpenMayaUI OpenMayaAnim OpenMayaFX OpenMayaRender Foundation ${PROJECT_NAME}Proto )
include( $ENV{DEVKIT_LOCATION}/cmake/pluginEntry.cmake )
build_plugin()
Currently, the code builds, and links, fine when running cmake .. and cmake --build . --clean-first from a build/ inside the project folder, but the plugin cannot be loaded. The following error is shown in the script editor:
// Error: file: C:/.../Autodesk/Maya2022/scripts/others/pluginWin.mel line 934: Unable to dynamically load : C:/.../maya_plugin/build/Debug/Hello.mll
The specified module could not be found.
// Error: file: C:/.../Autodesk/Maya2022/scripts/others/pluginWin.mel line 934: The specified module could not be found.
(Hello) //
// Warning: file: C:/.../Autodesk/Maya2022/scripts/others/pluginWin.mel line 937: Could not load C:/.../maya_plugin/build/Debug/Hello.mll as a plug-in //
Although Hello.mll is created in the Debug/, it seems that there is a run-time problem that prevents the plugin from being loaded. As a test, I commented out the definition of f1 from the initialize_plugin() function, and the plugin was loaded successfully.
Q1: What is causing this problem?
Q2: Is the CMakeLists.txt file properly written to load Protocol Buffers?
Please note, that the CMakeLists.txt should not create an executable, as it's a plugin. Also, I am currently running Windows 10 and I have successfully installed Google's Protocol Buffers via vcpkg. To test the installation of the library, I created a similar example only as an executable, and it works properly.

cmake dynamic library subproject

On a project of mine using c++ and cmake, I include several third party libraries, including SDL2.
As many before me, I'm struggling to get the application to find the libraries correctly to run.
So, as usual, if the libraries are installed in the system, SDL2 is found with find_package, and everything proceeds as normal.
( EDIT )This is ThirdParty.cmake:
cmake_minimum_required(VERSION 2.8)
include ( "ExternalProject" )
message ( STATUS "********** Starting third party dependency checks **********")
set ( THIRD_PARTY_OUTPUT_PATH "${CMAKE_CURRENT_LIST_DIR}/${CMAKE_SYSTEM_NAME}" )
list ( APPEND CMAKE_PREFIX_PATH ${THIRD_PARTY_OUTPUT_PATH} )
set ( THIRD_PARTY_DEPENDENCIES_OK TRUE )
# GLM
find_package ( GLM QUIET )
if ( NOT GLM_FOUND )
ExternalProject_Add ( ThirdParty_GLM
#GIT_REPOSITORY "https://github.com/g-truc/glm"
URL "https://github.com/g-truc/glm/archive/0.9.7.6.zip"
INSTALL_DIR ${THIRD_PARTY_OUTPUT_PATH}
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
)
set ( THIRD_PARTY_DEPENDENCIES_OK FALSE )
#add_subdirectory ( ThirdParty/glm )
#set ( GLM_INCLUDE_DIR "ThirdParty/glm" )
endif()
# SDL2
find_package ( SDL2 QUIET )
if ( NOT SDL2_FOUND )
ExternalProject_Add ( ThirdParty_SDL2
URL "https://www.libsdl.org/release/SDL2-2.0.5.zip"
INSTALL_DIR ${THIRD_PARTY_OUTPUT_PATH}
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
)
set ( THIRD_PARTY_DEPENDENCIES_OK FALSE )
# set ( SDL_SHARED_ENABLED_BY_DEFAULT FALSE )
# add_subdirectory ( ThirdParty/SDL2 )
# set ( SDL2_INCLUDE_DIR "ThirdParty/SDL2/include" )
# set ( SDL2_LIBRARY sdl2-static )
endif()
# FreeImage
find_package ( FreeImage QUIET )
if ( NOT FREEIMAGE_FOUND )
ExternalProject_Add ( ThirdParty_FreeImage
GIT_REPOSITORY "gitrepo:Misc/FreeImage"
INSTALL_DIR ${THIRD_PARTY_OUTPUT_PATH}
CMAKE_ARGS -DSDL_SHARED_ENABLED_BY_DEFAULT=FALSE -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
)
set ( THIRD_PARTY_DEPENDENCIES_OK FALSE )
# add_subdirectory ( ThirdParty/FreeImage )
# set ( FREEIMAGE_INCLUDE_DIR "ThirdParty/FreeImage/src" )
# set ( FREEIMAGE_LIBRARY freeimage )
endif()
# Assimp
find_package ( Assimp QUIET )
if ( NOT ASSIMP_FOUND )
ExternalProject_Add ( ThirdParty_Assimp
GIT_REPOSITORY "https://github.com/assimp/assimp"
GIT_TAG "v3.2"
INSTALL_DIR ${THIRD_PARTY_OUTPUT_PATH}
CMAKE_ARGS -DCMAKE_C_FLAGS=-fPIC -DCMAKE_CXX_FLAGS=-fPIC -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> "-DASSIMP_BUILD_TESTS=OFF"
)
set ( THIRD_PARTY_DEPENDENCIES_OK FALSE )
# set ( ASSIMP_BUILD_ASSIMP_TOOLS FALSE)
# set ( ASSIMP_BUILD_TESTS FALSE )
# set ( BUILD_SHARED_LIBS FALSE )
# add_subdirectory ( ThirdParty/Assimp )
# set ( ASSIMP_INCLUDE_DIR "ThirdParty/assimp/include" )
# set ( ASSIMP_LIBRARIES "assimp" )
endif()
if ( THIRD_PARTY_DEPENDENCIES_OK )
list ( APPEND ILLUSION_INCLUDE_DIRS ${SDL2_INCLUDE_DIR} )
list ( APPEND ILLUSION_INCLUDE_DIRS ${GLM_INCLUDE_DIR} )
list ( APPEND ILLUSION_INCLUDE_DIRS ${FREEIMAGE_INCLUDE_DIR} )
list ( APPEND ILLUSION_INCLUDE_DIRS ${ASSIMP_INCLUDE_DIR} )
list ( APPEND ILLUSION_LIBRARIES ${SDL2_LIBRARIES} )
list ( APPEND ILLUSION_LIBRARIES ${FREEIMAGE_LIBRARY} )
list ( APPEND ILLUSION_LIBRARIES ${ASSIMP_LIBRARIES} )
list ( APPEND ILLUSION_LINK_DIRS ${THIRD_PARTY_OUTPUT_PATH}/lib )
list ( APPEND ILLUSION_INCLUDE_DIRS ${THIRD_PARTY_OUTPUT_PATH}/include )
else()
message ( STATUS "Dependencies not ready. Building")
endif()
message ( STATUS "********** Finished third party dependency checks **********")
(EDIT) This is the full CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project ( Illusion )
########## Platform and target configuration ##########
string ( TOUPPER ${CMAKE_SYSTEM_NAME} UPPERCASE_SYSTEM_NAME )
message ( STATUS "Host building system - ${CMAKE_SYSTEM}" )
set ( ILLUSION_TARGET_PLATFORM ${UPPERCASE_SYSTEM_NAME} )
list ( APPEND ILLUSION_DEFINITIONS -DILLUSION_TARGET_PLATFORM_${UPPERCASE_SYSTEM_NAME} )
message ( STATUS "Target system - ${ILLUSION_TARGET_PLATFORM}" )
set ( ILLUSION_VERSION_MAJOR 0 )
set ( ILLUSION_VERSION_MINOR 4 )
set ( ILLUSION_ROOT ${PROJECT_SOURCE_DIR})
set ( ILLUSION_BUILTIN_DATA_PATH \"/usr/local/share/Illusion/\" )
list ( APPEND ILLUSION_INCLUDE_DIRS "${ILLUSION_ROOT}" )
set ( CMAKE_MODULE_PATH "${ILLUSION_ROOT}/ThirdParty/CMakeHelpers" ${CMAKE_MODULE_PATH})
set ( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${ILLUSION_TARGET_PLATFORM}/lib )
set ( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${ILLUSION_TARGET_PLATFORM}/lib )
set ( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${ILLUSION_TARGET_PLATFORM}/bin )
set ( CMAKE_INSTALL_RPATH ${THIRD_PARTY_OUTPUT_PATH}/lib )
set ( CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE )
########## Third party libraries ##########
include ( "ThirdParty/ThirdParty.cmake" )
add_definitions ( ${ILLUSION_DEFINITIONS} )
include_directories ( ${ILLUSION_INCLUDE_DIRS} )
if ( THIRD_PARTY_DEPENDENCIES_OK )
message ( STATUS "All dependencies present. Building Illusion")
########## Compiler configuration ##########
if ( "${CMAKE_BUILD_TYPE}" STREQUAL "" )
set ( CMAKE_BUILD_TYPE Debug )
message ( STATUS "Setting build type to debug as default" )
endif()
include ( "ThirdParty/CMakeHelpers/Macros.cmake" )
enable_all_warnings()
use_cpp11()
########## Choose graphics backend ##########
include ( "Graphics/Graphics.cmake" )
########## Main library ##########
configure_file (
"${PROJECT_SOURCE_DIR}/IllusionConfig.h.in"
"${PROJECT_SOURCE_DIR}/IllusionConfig.h"
)
file ( GLOB Illusion_Sources "*.cpp" "*.h" "Graphics/*.cpp" "Graphics/*.h" "Material/*.cpp" "Material/*.h" "Camera/*.cpp" "Camera/*.h" "Model/*.cpp" "Model/*.h" "Asset/*.cpp" "Asset/*.h " "FileSystem/*.cpp" "FileSystem/*.h" )
file ( GLOB_RECURSE Pipeline_Sources "Pipelines/*" )
file ( GLOB_RECURSE DataFiles "Data/*" FindIllusion.cmake TODO.txt )
add_library ( WindowSystem "WindowSystem/WindowSystem.cpp" "WindowSystem/WindowSystem.h" )
add_library ( Illusion
${Illusion_Sources}
${DataFiles}
${Pipeline_Sources}
)
########## Submodules ##########
add_subdirectory ( StringFunctions )
add_subdirectory ( ValueFileParser )
list ( APPEND ILLUSION_LIBRARIES ${GraphicsBackendLibraries} WindowSystem ${SDL2_LIBRARIES} ValueFileParser StringFunctions )
# add a target to generate API documentation with Doxygen
find_package ( Doxygen )
if ( DOXYGEN_FOUND )
configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile #ONLY )
add_custom_target ( doc
${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen" VERBATIM
)
endif ()
target_link_libraries ( Illusion ${ILLUSION_LIBRARIES} )
# Just to check if i have a parent
get_directory_property ( hasParent PARENT_DIRECTORY )
if ( hasParent )
message ( STATUS "Passing Illusion variables to parent project" )
set ( ILLUSION_INCLUDE_DIRS ${ILLUSION_INCLUDE_DIRS} PARENT_SCOPE )
set ( ILLUSION_LIBRARIES Illusion ${ILLUSION_LIBRARIES} PARENT_SCOPE )
set ( ILLUSION_LINK_DIRS ${ILLUSION_LINK_DIRS} PARENT_SCOPE )
set ( ILLUSION_DEFINITIONS ${ILLUSION_DEFINITIONS} PARENT_SCOPE )
endif()
option ( ILLUSION_BUILD_TESTS "Builds tests" TRUE )
if ( ILLUSION_BUILD_TESTS )
message ( STATUS "Building tests" )
file ( GLOB Test_Sources "Tests/*.cpp" "Tests/*.h" )
add_executable ( Test ${Test_Sources} )
target_link_libraries ( Test Illusion )
add_executable ( WindowSystemTest WindowSystem/WindowSystemTest.cpp )
target_link_libraries ( WindowSystemTest WindowSystem ${SDL2_LIBRARY} )
add_custom_command ( TARGET Test POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory "${THIRD_PARTY_OUTPUT_PATH}/lib" ${CMAKE_LIBRARY_OUTPUT_DIRECTORY} )
endif()
# include ( "Package.cmake" )
endif()
This works of for building. The first time I run cmake, it builds all the dependencies, but not the main project. The second time I run it, it correctly finds the libraries, and builds the main project. If I look into the output folder and inside lib, all the third party libraries are correctly copied inside there. All is well on that part. The main problem comes with linking. When I try to run the test application, I get this...
dyld: Library not loaded: libSDL2-2.0.1.dylib
Referenced from: /Users/joao.pincho/Programming/build-Illusion_deps-Desktop_Qt_5_7_0_clang_64bit-Default/DARWIN/bin/./Test
Reason: image not found
Trace/BPT trap: 5
Of course, this works if I specify DYLD_LIBRARY_PATH pointing to THIRD_PARTY_OUTPUT_PATH/lib, but that prevents me from running this on the IDE, and will also not work under Windows...
I intend to make this build process work under Linux, OSX, Windows, Android and IOS, so I have that restriction to think ahead of.
So, since in Windows I need to have the DLL's and libs all together, I might as well copy everything into the same directory.
As a small test, I copied the test executable into the libs folder, where everything is put.
This is what I get:
dyld: Library not loaded: /Users/joao.pincho/Programming/Illusion_deps/ThirdParty/Darwin//libassimp.3.dylib
Referenced from: /Users/joao.pincho/Programming/build-Illusion_deps-Desktop_Qt_5_7_0_clang_64bit-Default/DARWIN/lib/./Test
Reason: image not found
Trace/BPT trap: 5
And this is the part I honestly do not understand. Why is it looking for libassimp in the wrong folder?
(EDIT) The correct path where the file was copied from is /Users/joao.pincho/Programming/Illusion_deps/ThirdParty/Darwin/lib/libassimp.3.dylib
What can I do to just make this work? Why is it looking for the libs in the wrong place? Why did it stop complaining about libSDL2 when I moved the executable, but not about libassimp?

CMake $<TARGET_FILE:tgt> generator expression along with DeployQt5

I am trying to deploy my Qt-based app under CMake. I used to use DeployQt5 in that way:
if (APPLE)
set (QT_QPA_PLUGIN ${QT_INSTALL_PLUGINS}/platforms/libqcocoa.dylib)
set (tgt_EXECUTABLE tgt.app)
elseif (WIN32 AND MSVC)
set (QT_QPA_PLUGIN ${QT_INSTALL_PLUGINS}/platforms/qwindows.dll)
get_property (tgt_EXECUTABLE TARGET tgt PROPERTY LOCATION)
get_filename_component (tgt_EXECUTABLE ${tgt_EXECUTABLE} NAME)
endif ()
set(CMAKE_INSTALL_SYSTEM_RUNTIME_DESTINATION ".")
include(InstallRequiredSystemLibraries)
include (DeployQt5)
install_qt5_executable ("${tgt_EXECUTABLE}"
"${QT_QPA_PLUGIN}" # qtplugins
"" # libs
"${QT_INSTALL_LIBS}" # dirs
)
but I don't like it. I want to use cmake's generator expressions and abandon the LOCATION property. I wonder if it is possible with the DeployQt5 module. It does not seem to be that straightforward:
include (DeployQt5)
install_qt5_executable ("$<TARGET_FILE:tgt>"
"${QT_QPA_PLUGIN}" # qtplugins
"" # libs
"${QT_INSTALL_LIBS}" # dirs
)
doesn't work. Is it possible to make that part of the CMakeLists.txt file cleaner?

Adding compiled library to CMake Qt Project?

I have been trying to include the latest version of qextserialport into the project I am working on. The project structure is as below:
CameraProject
CameraProject.pro
CameraProject
Headers
Sources
qextserialport
Headers
Sources
My CMake file currently looks like so:
PROJECT(CameraProject)
FIND_PACKAGE(Qt4 REQUIRED)
FIND_LIBRARY(SIMPLONLIB lv.simplon lib)
FIND_LIBRARY(SIMPLONIMGPROC lv.simplon.imgproc lib)
SET(CameraProject_SOURCES include/lv.simplon.class.cpp camera.cpp main.cpp mainwindow.cpp osdep.cpp paint.cpp)
SET(CameraProject_HEADERS include/lv.simplon.class.h camera.h mainwindow.h osdep.h paint.h)
SET(CameraProject_RESOURCES icons.qrc)
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})
ADD_EXECUTABLE(CameraProject ${CameraProject_SOURCES}
${CameraProject_HEADERS_MOC}
${CameraProject_RESOURCES_RCC})
TARGET_LINK_LIBRARIES(CameraProject ${QT_LIBRARIES} ${SIMPLONLIB} ${SIMPLONIMGPROC})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} include QExtSerialPort/src)
And despite including the QextSerialPort files in the Include Directories, I get linker issues being drawn.
EDIT: More information!
Found here is the qextserialport.pri file that is included in CameraProject.pro like so: include(./QExtSerialPort/src/qextserialport.pri)
Should I be making a second CMakeLists file for the qextserialport library placed within the source folder?
Any input would be greatly appreciated, please do not hesitate to request for clarification or any further information.
What did for qxt is to use a cmake finder module instead of directly including the code into my projects. I then built qxt with qmake and let CMake find the libraries it creates.
#############
## basic FindQxt.cmake
## This is an *EXTREMELY BASIC* cmake find/config file for
## those times you have a cmake project and wish to use
## libQxt.
##
## It should be noted that at the time of writing, that
## I (mschnee) have an extremely limited understanding of the
## way Find*.cmake files work, but I have attempted to
## emulate what FindQt4.cmake and a few others do.
##
## To enable a specific component, set your QXT_USE_${modname}:
## SET(QXT_USE_QXTCORE TRUE)
## SET(QXT_USE_QXTGUI FALSE)
## Currently available components:
## QxtCore, QxtGui, QxtNetwork, QxtWeb, QxtSql
## Auto-including directories are enabled with INCLUDE_DIRECTORIES(), but
## can be accessed if necessary via ${QXT_INCLUDE_DIRS}
##
## To add the libraries to your build, TARGET_LINK_LIBRARIES(), such as...
## TARGET_LINK_LIBRARIES(YourTargetNameHere ${QXT_LIBRARIES})
## ...or..
## TARGET_LINK_LIBRARIES(YourTargetNameHere ${QT_LIBRARIES} ${QXT_LIBRARIES})
################### TODO:
## The purpose of this cmake file is to find what components
## exist, regardless of how libQxt was build or configured, thus
## it should search/find all possible options. As I am not aware
## that any module requires anything special to be used, adding all
## modules to ${QXT_MODULES} below should be sufficient.
## Eventually, there should be version numbers, but
## I am still too unfamiliar with cmake to determine how to do
## version checks and comparisons.
## At the moment, this cmake returns a failure if you
## try to use a component that doesn't exist. I don't know how to
## set up warnings.
## It would be nice having a FindQxt.cmake and a UseQxt.cmake
## file like done for Qt - one to check for everything in advance
##############
###### setup
SET(QXT_MODULES QxtGui QxtWeb QxtZeroConf QxtNetwork QxtSql QxtBerkeley QxtCore)
SET(QXT_FOUND_MODULES)
FOREACH(mod ${QXT_MODULES})
STRING(TOUPPER ${mod} U_MOD)
SET(QXT_${U_MOD}_INCLUDE_DIR NOTFOUND)
SET(QXT_${U_MOD}_LIB_DEBUG NOTFOUND)
SET(QXT_${U_MOD}_LIB_RELEASE NOTFOUND)
SET(QXT_FOUND_${U_MOD} FALSE)
ENDFOREACH(mod)
SET(QXT_QXTGUI_DEPENDSON QxtCore)
SET(QXT_QXTWEB_DEPENDSON QxtCore QxtNetwork)
SET(QXT_QXTZEROCONF_DEPENDSON QxtCore QxtNetwork)
SET(QXT_QXTNETWORK_DEPENDSON QxtCore)
SET(QXT_QXTQSQL_DEPENDSON QxtCore)
SET(QXT_QXTBERKELEY_DEPENDSON QxtCore)
FIND_PATH(QXT_DIR libqxt.pro Qxt/include/QxtCore/Qxt)
FIND_PATH(QXT_BINARY_DIR
NAMES QxtCore.dll QxtCored.dll
PATHS
${QXT_DIR}/bin
${QXT_DIR}/Bin
NO_DEFAULT_PATH
)
#SET(QXT_BINARY_DIR "${QXT_DIR}/bin" CACHE PATH "${QXT_DIR}/bin")
FOREACH(mod ${QXT_MODULES})
STRING(TOUPPER ${mod} U_MOD)
FIND_PATH(QXT_${U_MOD}_INCLUDE_DIR ${mod}
PATH_SUFFIXES ${mod} include/${mod} Qxt/include/${mod} include/Qxt/${mod}
PATHS
~/Library/Frameworks/
/Library/Frameworks/
/sw/
/usr/local/
/usr
/opt/local/
/opt/csw
/opt
"C:\\"
"C:\\Program Files\\"
"C:\\Program Files(x86)\\"
${QXT_DIR}
NO_DEFAULT_PATH
)
FIND_LIBRARY(QXT_${U_MOD}_LIB_RELEASE NAME ${mod}
PATH_SUFFIXES Qxt/lib64 Qxt/lib lib64 lib
PATHS
/sw
/usr/local
/usr
/opt/local
/opt/csw
/opt
"C:\\"
"C:\\Program Files"
"C:\\Program Files(x86)"
${QXT_DIR}
NO_DEFAULT_PATH
)
FIND_LIBRARY(QXT_${U_MOD}_LIB_DEBUG NAME ${mod}d
PATH_SUFFIXES Qxt/lib64 Qxt/lib lib64 lib
PATHS
/sw
/usr/local
/usr
/opt/local
/opt/csw
/opt
"C:\\"
"C:\\Program Files"
"C:\\Program Files(x86)"
${QXT_DIR}
NO_DEFAULT_PATH
)
IF (QXT_${U_MOD}_LIB_RELEASE)
SET(QXT_FOUND_MODULES "${QXT_FOUND_MODULES} ${mod}")
ENDIF (QXT_${U_MOD}_LIB_RELEASE)
IF (QXT_${U_MOD}_LIB_DEBUG)
SET(QXT_FOUND_MODULES "${QXT_FOUND_MODULES} ${mod}")
ENDIF (QXT_${U_MOD}_LIB_DEBUG)
ENDFOREACH(mod)
FOREACH(mod ${QXT_MODULES})
STRING(TOUPPER ${mod} U_MOD)
IF(QXT_${U_MOD}_INCLUDE_DIR AND QXT_${U_MOD}_LIB_RELEASE)
SET(QXT_FOUND_${U_MOD} TRUE)
ENDIF(QXT_${U_MOD}_INCLUDE_DIR AND QXT_${U_MOD}_LIB_RELEASE)
ENDFOREACH(mod)
##### find and include
# To use a Qxt Library....
# SET(QXT_FIND_COMPONENTS QxtCore, QxtGui)
# ...and this will do the rest
IF( QXT_FIND_COMPONENTS )
FOREACH( component ${QXT_FIND_COMPONENTS} )
STRING( TOUPPER ${component} _COMPONENT )
SET(QXT_USE_${_COMPONENT}_COMPONENT TRUE)
ENDFOREACH( component )
ENDIF( QXT_FIND_COMPONENTS )
SET(QXT_LIBRARIES "")
SET(QXT_INCLUDE_DIRS "")
# like FindQt4.cmake, in order of dependence
FOREACH( module ${QXT_MODULES} )
STRING(TOUPPER ${module} U_MOD)
IF(QXT_USE_${U_MOD} OR QXT_DEPENDS_${U_MOD})
IF(QXT_FOUND_${U_MOD})
STRING(REPLACE "QXT" "" qxt_module_def "${U_MOD}")
ADD_DEFINITIONS(-DQXT_${qxt_module_def}_LIB)
SET(QXT_INCLUDE_DIRS ${QXT_INCLUDE_DIRS} ${QXT_${U_MOD}_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${QXT_${U_MOD}_INCLUDE_DIR})
SET(QXT_LIBRARIES ${QXT_LIBRARIES};optimized;${QXT_${U_MOD}_LIB_RELEASE};debug;${QXT_${U_MOD}_LIB_DEBUG})
ELSE(QXT_FOUND_${U_MOD})
MESSAGE("Could not find Qxt Module ${module}")
RETURN()
ENDIF(QXT_FOUND_${U_MOD})
FOREACH(dep QXT_${U_MOD}_DEPENDSON)
SET(QXT_DEPENDS_${dep} TRUE)
ENDFOREACH(dep)
ENDIF(QXT_USE_${U_MOD} OR QXT_DEPENDS_${U_MOD})
ENDFOREACH(module)
MESSAGE(STATUS "Found Qxt Libraries:${QXT_FOUND_MODULES}")
MESSAGE(STATUS "Include directories:${QXT_INCLUDE_DIRS}")
I put this module in a sub directory of my root project called CMake/Modules and include it into cmake near the top of the root CMakeLists.txt using the following command:
# The following line will add additional finders to CMake without the need to be placed in the CMake install path
LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake/Modules)
And finally to have CMake find Qxt
SET(QXT_FIND_COMPONENTS QxtCore, QxtGui)
SET(QXT_USE_QXTCORE TRUE)
FIND_PACKAGE(Qxt REQUIRED)

cmake shared libraries

I'm building app with boost.python. I want to make each cpp-module as a single shared library.
I have a list of cpp-files, how to make in cycle different subprojects with name taken from cpp file?
For example:
set(Script_srcs
Module1.cpp
Module2.cpp
Module3.cpp
)
some_cycle
add_library( {NAME} SHARED {PATH} )
Thanks.
You could use foreach to iterate over your source-files and use a the function get_filename_component with NAME_WE to get only the filename, something like:
set(Script_srcs Module1.cpp Module2.cpp Module3.cpp )
foreach( f ${Script_srcs} )
get_filename_component( ff ${f} NAME_WE )
add_library( ${ff} SHARED ${f} )
endforeach( f )
But, instead of deriving library-names from filenames, why not work the other way around? Derive your filenames from the desired module-names and append ".cpp" in the for-loop:
set( MyModules Module1 Module2 Module3 )
foreach( Mod ${MyModules} )
add_library( ${Mod} SHARED ${Mod}.cpp )
endforeach( Mod )