Add subdirectories and use VTK library and Qt in my projects. The file structure looks like the following image.



projectB generates lib and dll files which will be used by projectA. projectA show a window and print message from Base class in projectB.

The file CMakeLists.txt in the root directionary cmakeAddSubdirectory.

cmake_minimum_required(VERSION 2.8)

project(cmakeAddSubdirectory)

if(POLICY CMP0043)
  cmake_policy(SET CMP0043 NEW) # CMake 3.0
endif()
if(POLICY CMP0020)
  cmake_policy(SET CMP0020 NEW)
endif()

set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)

add_subdirectory(projectA)
add_subdirectory(projectB)

CMakeLists.txt in projectA.

cmake_minimum_required(VERSION 2.8)

project(projectA)

if(POLICY CMP0043)
  cmake_policy(SET CMP0043 NEW) # CMake 3.0
endif()
if(POLICY CMP0020)
  cmake_policy(SET CMP0020 NEW)
endif()

find_package( VTK REQUIRED )
include( ${VTK_USE_FILE} )

#find_path(HearderPath Hearder_Path)

# add header files which are generated by qt
include_directories( ${CMAKE_CURRENT_BINARY_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/../projectB)

file(GLOB UI_FILES *.ui)
file(GLOB HEADER_FILES
    *.h
    *.hpp
    )
file(GLOB CXX_FILES
    *.cxx
    *.cpp
    )

set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)

link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../projectB/bin)
message("=== lib dir ==> ${CMAKE_CURRENT_SOURCE_DIR}/../projectB/bin/")

qt5_wrap_ui(UISrcs ${UI_FILES} )
# CMAKE_AUTOMOC in ON so the MOC headers will be automatically wrapped.
set( CMAKE_AUTOMOC ON )
add_executable(${PROJECT_NAME} MACOSX_BUNDLE ${HEADER_FILES} ${CXX_FILES} ${UISrcs} )
qt5_use_modules(${PROJECT_NAME} Core Gui)

target_link_libraries( ${PROJECT_NAME} ${VTK_LIBRARIES} projectB)

if (WIN32)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
endif(WIN32)

CMakeLists.txt in projectB. We will set compiler to support standard C++ 17.

cmake_minimum_required(VERSION 2.8.8)
if(POLICY CMP0020)
  cmake_policy(SET CMP0020 NEW)
endif()
if(POLICY CMP0025)
  cmake_policy(SET CMP0025 NEW) # CMake 3.0
endif()
if(POLICY CMP0043)
  cmake_policy(SET CMP0043 NEW) # CMake 3.0
endif()
if(POLICY CMP0053)
  cmake_policy(SET CMP0053 NEW) # CMake 3.1
endif()

# =========== turn on C++17 ===========
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if ( CMAKE_SYSTEM_NAME MATCHES "Windows" )
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++lastest")
else ()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std:c++lastest")
endif()
# =========== finish: turn on C++17 ===========

set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})

project(projectB)
find_package(VTK NO_MODULE)

include(${VTK_USE_FILE})

#find_path(HearderPath Hearder_Path)
#set( projectBHDir  ${HearderPath}/projectB)
#message( "=========> ${projectBHDir}" )

include_directories(
  ${CMAKE_CURRENT_BINARY_DIR}
  ${CMAKE_CURRENT_SOURCE_DIR}
 )

# Set your files and resources here
set( Srcs 
  Base.cpp )
set( Hdrs
  PreDefinition.hpp
  Base.h
        )


#add_definitions( 
#   -DCLASSEXPORT
#)

set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)

message("==== projectB ===> ${CMAKE_CURRENT_SOURCE_DIR}")

if (APPLE)
   add_library(${PROJECT_NAME} STATIC ${Srcs} ${Hdrs})
   target_link_libraries(projectB ${VTK_LIBRARIES} )
else () #WINDOWS
   add_library(${PROJECT_NAME} SHARED ${Srcs} ${Hdrs})
   target_link_libraries(projectB ${VTK_LIBRARIES} )
endif(APPLE)


if (WIN32)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
endif(WIN32)

#foreach( _header ${Hdrs} )
# STRING(REGEX MATCH "(.*)[/\]" DIR ${_header})
#        file(INSTALL ${_header} DESTINATION ${projectBHDir}/${DIR})
#endforeach( _header )

Output:

λ .\projectA.exe
Debug: Off
Modified Time: 3
Reference Count: 1
Registered Events: (none)
hello~

All source code on GitHub: cmakeAddSubdirectory


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Image Processing: support to flip your image.

X
A prohibited operation
0
Would love your thoughts, please comment.x
()
x