Code Monkey home page Code Monkey logo

cmake-templates's Introduction

To c++/cmake users:


cmake-templates

Some CMake Templates.

1. Overview

Conventions

  • 😄 means tested okay/good
  • 😡 means test result not sufficiently good
  • ❓ means not tested yet
Project Linux + GCC 4.8+ Win + VS2010 Win + VS2015 macOS
c 😄 😄 😄 😄
c++ 😄 😄 😄 😄
c++11 😄 😡 😄 😄
c++11vs2010 😄 😄 😄 😄
module 😄 😄 😄 😄
opencv 😄 😡
opencv3 😄 😄 😄
boost 😄 😄 😄
qt4-console 😄 😄 😄 😡
qt4-gui 😄 😄 😄 😡
qt4-project 😄 😄 😄 😡
qt5-project 😄 😄 😄

2. Usage

2.1. Windows

Use CMake-GUI to generate Visual Studio 2010 project, then use Visual Studio to compile & run.

Here is a Tutorial: HOWTO: Win + CMake + Visual Studio 2010 · Issue #1 · district10/cmake-templates.

2.2. Linux

Most commonly, we build Makefile project:

# cd source dir (there should be a CMakeLists.txt)
mkdir build && cd build
cmake ..            # want a release build? try `cmake -DCMAKE_BUILD_TYPE=Release ..'
make

# then checkout the generated binary files

But we can build CodeBlocks projects too, see my tutorial: HOWTO: Linux + CMake + CodeBlocks + GNU Make · Issue #2 · district10/cmake-templates, or use qt-creator to open CMakeLists.txt directly, see my tutorial: HOWTO: Use Qt creator to Open CMakeLists.txt directly (will generate proper project files) · Issue #5 · district10/cmake-templates.

3. Examples

3.1. C Example

Simple C project.

project( C )
cmake_minimum_required( VERSION 2.6 )
add_executable( ${PROJECT_NAME} main.c )

cmake_minimum_required( ... ) is needed in root CMakeLists.txt, always.

The ${PROJECT_NAME} is variable with value C, which is set by the project( C ).

See c.

3.2. C++ Example

Simple C++ project.

project( CPP )
make_minimum_required( VERSION 2.6 )
file( GLOB SRCS *.c *.cpp *.cc *.h *.hpp )  # a variable called SRCS with all files whose path match "*.c *.cpp..."
add_executable( ${PROJECT_NAME} ${SRCS} )

See cpp.

3.3. C++11 Example

C++11 project.

include( CheckCXXCompilerFlag )
check_cxx_compiler_flag( "-std=c++11"   COMPILER_SUPPORTS_CXX11 )
check_cxx_compiler_flag( "-std=c++0x"   COMPILER_SUPPORTS_CXX0X )
if( COMPILER_SUPPORTS_CXX11 )
    if( CMAKE_COMPILER_IS_GNUCXX )
        set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11" )
    else()
        set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
    endif()
elseif( COMPILER_SUPPORTS_CXX0X)
    set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x" )
else()
    # MSVC, On by default (if available)
endif()

See cpp11.

I recommend Visual Studio 2015 Community Edition.

3.4. Example to Show How to Modualize Your Project

# root cmakelists.txt
project( MODULES )
cmake_minimum_required( VERSION 2.8.3 )

include_directories( ${CMAKE_SOURCE_DIR}/includes )

add_subdirectory( src )
add_subdirectory( demo )

# src dir
add_subdirectory( cubic )
add_subdirectory( extras )
add_subdirectory( square )

# cubic
add_library( LibCubic ${CUBICS} cubic.c )

# demo
project( CALC )
cmake_minimum_required( VERSION 2.6 )

set( EXTRA_LIBS ${EXTRA_LIBS} LibSquare )
set( EXTRA_LIBS ${EXTRA_LIBS} LibExtras )
set( EXTRA_LIBS ${EXTRA_LIBS} LibCubic )

add_executable( Calc calc.c )
target_link_libraries( Calc  ${EXTRA_LIBS} )

See modules.

3.5. Example with Support of Boost

project( BOOST )
cmake_minimum_required( VERSION 2.6 )

find_package( Boost REQUIRED )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
LINK_DIRECTORIES( ${Boost_LIBRARY_DIRS} )
set( Boost_USE_STATIC_LIBS        OFF )
set( Boost_USE_MULTITHREADED      ON )
set( Boost_USE_STATIC_RUNTIME     OFF )
set( BOOST_ALL_DYN_LINK           ON ) # force dynamic linking for all libraries

add_executable( ${PROJECT_NAME} main.cpp )
target_link_libraries( ${PROJECT_NAME} ${Boost_LIBRARIES} )

Ubuntu install: sudo apt-get install libboost-all-dev.

See boost.

3.6. Example with Support of OpenCV

Want to how to configure both opencv 2 & 3 on your system? Checkout my tutorial: HOWTO: OpenCV 2 & OpenCV 3 · Issue #4 · district10/cmake-templates.

opencv 2 or less

project( OPENCV )
cmake_minimum_required( VERSION 2.6 )

include( $ENV{OpenCV2_DIR}/OpenCVConfig.cmake ) # find_package( OpenCV REQUIRED )

message( STATUS "OpenCV library status:" )
message( STATUS "    version: ${OpenCV_VERSION}" )
message( STATUS "    libraries: ${OpenCV_LIBS}" )
message( STATUS "    include path: ${OpenCV_INCLUDE_DIRS}" )

include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable( ${PROJECT_NAME}  minarea.c )
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS} )

opencv 3

project( OPENCV3 )
cmake_minimum_required( VERSION 2.8 )

include( $ENV{OpenCV3_DIR}/OpenCVConfig.cmake ) # find_package( OpenCV REQUIRED )

message( STATUS "OpenCV library status:" )
message( STATUS "    version: ${OpenCV_VERSION}" )
message( STATUS "    libraries: ${OpenCV_LIBS}" )
message( STATUS "    include path: ${OpenCV_INCLUDE_DIRS}" )

include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable( ${PROJECT_NAME} example.cpp )
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS} )

See

  • opencv: for opencv2 or less (VS2010 😄, Linux ❓)
  • opencv3: for opencv3 (VS2010 ❓, Linux 😄)

3.7. Example with Support of Qt4

Be sure to make qmake caught by CMake, put it in your $PATH.

qt4 console

find_package( Qt4 REQUIRED )
include( ${QT_USE_FILE} )
set( QT_DONT_USE_QTGUI TRUE )

add_executable( ${PROJECT_NAME} main.cpp )
target_link_libraries( ${PROJECT_NAME}  ${QT_LIBRARIES} )

configure file

configure_file(
    "${PROJECT_SOURCE_DIR}/Configs.h.in"
    "${PROJECT_BINARY_DIR}/Configs.h" )

moc, uic

file( GLOB_RECURSE HDRS_FILES *.h *.hpp )
file( GLOB_RECURSE SRCS_FILES *.cpp )
file( GLOB_RECURSE UI_FILES *.ui )

qt4_wrap_cpp( MOC_SRCS ${HDRS_FILES} )
qt4_wrap_ui( UI_HDRS ${UI_FILES} )

source_group( "UI Files" FILES ${UI_FILES} )
source_group( "Generated Files" FILES ${MOC_SRCS} ${UI_HDRS} )

add_library( ${PROJECT_NAME} STATIC ${SRCS_FILES} ${UI_FILES} ${HDRS_FILES} ${MOC_SRCS} ${UI_HDRS} )
target_link_libraries( ${PROJECT_NAME} ${QT_LIBRARIES} )

Works like qmake -project, one ring to rule them all:

project( QT4 )
cmake_minimum_required( VERSION 2.6 )

find_package( Qt4 REQUIRED )
include( ${QT_USE_FILE} )

include_directories( ${CMAKE_SOURCE_DIR}/ )
include_directories( ${CMAKE_BINARY_DIR}/ )

# based on: https://cmake.org/Wiki/CMakeMacroFilterOut
macro( filter_out FILTERS INPUTS OUTPUTS )
    set( FOUT "" )
    foreach( INP ${INPUTS} )
        set( FILTERED 0 )
        foreach( FILT ${FILTERS} )
            if( ${FILTERED} EQUAL 0 )
                if( "${FILT}" STREQUAL "${INP}" )
                    set( FILTERED 1 )
                endif( "${FILT}" STREQUAL "${INP}" )
                if( ${INP} MATCHES ${FILT} )
                    set( FILTERED 1 )
                endif( ${INP} MATCHES ${FILT} )
            endif( ${FILTERED} EQUAL 0 )
        endforeach( FILT ${FILTERS} )
        if( ${FILTERED} EQUAL 0 )
            set( FOUT ${FOUT} ${INP} )
        endif( ${FILTERED} EQUAL 0 )
    endforeach( INP ${INPUTS} )
    set( ${OUTPUTS} ${FOUT} )
endmacro( filter_out FILTERS INPUTS OUTPUTS )

file( GLOB_RECURSE UI_FILES *.ui )
file( GLOB_RECURSE HDRS_FILES *.h *.hpp )
file( GLOB_RECURSE SRCS_FILES *.cpp *.c )
file( GLOB_RECURSE RCS_FILES *.qrc )

set( FILTERS ".*CompilerId.*" )
set( FILTERS ".*CMakeFiles/.*" )
filter_out("${FILTERS}" "${SRCS_FILES}" SRCS_FILES )

qt4_wrap_cpp( MOC_SRCS ${HDRS_FILES} )
qt4_wrap_ui( UI_HDRS ${UI_FILES} )
qt4_add_resources( RCS ${RCS_FILES} )

source_group( "UI Files" FILES ${UI_FILES} )
source_group( "Generated Files" FILES ${MOC_SRCS} ${UI_HDRS} )
source_group( "All Resource Files" FILES ${RCS} )

add_executable( ${PROJECT_NAME}
    ${MOC_SRCS}
    ${HDRS_FILES}
    ${SRCS_FILES}
    ${UI_FILES}
    ${UI_HDRS} ${RCS} )
target_link_libraries( ${PROJECT_NAME} ${QT_LIBRARIES} )

See

3.8. Example with Support of Qt5

project( Qt5Project )
cmake_minimum_required( VERSION 2.8.11 )

#                                           root of your msvc14 x64 prebuild
set( CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "C:/Qt/Qt5-msvc14/5.6/msvc2015_64" )
set( CMAKE_INCLUDE_CURRENT_DIR ON )
set( CMAKE_AUTOMOC ON )

find_package( Qt5Widgets REQUIRED )
qt5_wrap_ui( UI_HEADERS mainwindow.ui )
qt5_add_resources( QRCS resources.qrc )
add_executable( ${PROJECT_NAME} main.cpp mainwindow.cpp ${UI_HEADERS} ${QRCS} )
target_link_libraries( ${PROJECT_NAME} Qt5::Widgets )

See qt5 project.

3.9. Get'em Together (advanced examples)

This part is called CMake in Action.

4. TODO

  • More documentation
  • More elegant & illustrative examples
  • Planned Examples
    • for Windows, link *.lib files
    • for Linux, link *.a, *.so files, set rpath
    • etc.

5. Snippets & Helper Functions

cpp -> exe

file( GLOB SRCS src/*.cpp)
foreach( src ${SRCS} )
    string( REGEX REPLACE "(^.*/|.cpp$)" "" exe ${src} )
    message( STATUS "${exe} <-- ${src}" )
    add_executable( ${exe} ${src} )
endforeach( src )

There are some utility functions in utilities.cmake, use include(utilities.cmake) to include, then use

  • print_include_directories() to print all included directories,
  • print_all_linked_libraries(your_exe_or_lib) to print all linked libs,
  • print_all_variables() to print all variables

Tip, use cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=YES .. to generate compile commands (a json file).

6. ReadingList

These links may be useful:

7. Koan

  • CMake's documentation is not for human. It really smells
  • Adapt to various standards is by no means easy, it's kind of brain fucking

cmake-templates's People

Contributors

district10 avatar dvorak4tzx avatar jadnohra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cmake-templates's Issues

cmake project doesn't work

I make the build directory:

mkdir build && cd build

I do cmake ..

And I obtain the next ERROR:

CMake Error: The source directory "~/git/cmake-templates" does not appear to contain CMakeLists.txt.

The instructions are USELESS. Please REVIEW it BEFORE publish any USELESS solution!!!

HOWTO: macOS

Install Libs

$ brew install cmake boost qt5 glew opencv

Toggle Qt4 / Qt5

# 4 -> 5
$ brew unlink qt@4
$ brew link qt --force

# 5 -> 4
$ brew unlink qt
Unlinking /usr/local/Cellar/qt/5.9.1... 406 symlinks removed
$ brew link qt@4 --force
Linking /usr/local/Cellar/qt@4/4.8.7_2... 666 symlinks created

HOWTO: OpenCV 2 & OpenCV 3

Windows

When you find_package( OpenCV REQUIRED ) in CMakeLists.txt, it just look for OpenCVConfig.cmake under OpenCV_DIR, aka include( $ENV{OpenCV_DIR}/OpenCVConfig.cmake ).

If you want to use OpenCV2 & OpenCV3 in your system at the same time. Here is my solution:

  • VS2010 + OpenCV2 (there is an official prebuild for VS2010)
  • VS2015 + OpenCV3 (there is an official prebuild for VS2015)

1.

First goto OpenCV official download page, download opencv-2.4.x.exe and opencv-3.1.x.exe.

2.

Second, extract them, set these two proper environment variables:

  • OpenCV2_DIR -> D:\OpenCV\build
  • OpenCV3_DIR -> D:\OpenCV3\build
  • OpenCV_DIR -> %OpenCV_DIR% or %OpenCV3_DIR% (which you prefer)

add to PATH:

  • %OpenCV2_DIR%\x64\vc10\bin (for VS2010 generated exe)
  • %OpenCV3_DIR%\x64\vc14\bin (for VS2015 generated exe)

reboot may needed.

3.

Last, generated VS2010, VS2015 project to use OpenCV 2, OpenCV 3 with CMake, respectively.

You can see opencv2 / opencv3 examples in:

HOWTO: Linux + CMake + CodeBlocks + GNU Make

1. Build CodeBlocks Project

# goto source dir
cd module
mkdir build && cd build
cmake -G"CodeBlocks - Unix Makefiles" ..

generated codeblocks project

tip:

you can run cmake to get all your possible generators:

Generators

The following generators are available on this platform:
  Unix Makefiles              = Generates standard UNIX makefiles.
  Ninja                       = Generates build.ninja files (experimental).
  CodeBlocks - Ninja          = Generates CodeBlocks project files.
  CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
  Eclipse CDT4 - Ninja        = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Unix Makefiles
                              = Generates Eclipse CDT 4.0 project files.
  KDevelop3                   = Generates KDevelop 3 project files.
  KDevelop3 - Unix Makefiles  = Generates KDevelop 3 project files.
  Sublime Text 2 - Ninja      = Generates Sublime Text 2 project files.
  Sublime Text 2 - Unix Makefiles
                              = Generates Sublime Text 2 project files.

It seems sublime is a great choice. I'll cover that section later.

Build Executable & Run in CodeBlocks

build & run in codeblocks

because the building process is based on Makefile, you can also run these qt examples,
like:

example qt-gui

HOWTO: Win + CMake + Visual Studio 2010

Caution: If you don't have Qt4 configured, use cpp instead of qt4-project. The work-through is the same.

Tip: Better to use VS2015. (I'll leave the tutorial here 'cause I don't have time to re-screenshot.)

1. Drag CMakeLists.txt to CMakeGUI

2. Build Visual Studio Solution

3. Go to build dir, and open *.sln with Visual Studio

4. Set StartUp Project

5. Build & Run

6. Finally, you did it

HOWTO: Integrate Boost Library to Your Own Project

Boost is great, for it's powerful. Boost is bad, for it's way to BIG!

You can't expect other people to configure this gaint library just for compiling your code. Here is a solution: bcp.

BCP - 1.61.0

The bcp utility is a tool for extracting subsets of Boost, it's useful for Boost authors
who want to distribute their library separately from Boost, and for Boost users who
want to distribute a subset of Boost with their application.

bcp can also report on which parts of Boost your code is dependent on, and what
licences are used by those dependencies.

Here is my fork of bcp to demonstrate how to extract code from Boost and make a static compilation againt your own MiniBoost: district10/bcp at standalone.

Another example is my fork of @satoren's kaguya: 4ker/kaguya at standalone. Kaguya is a lua C++ binding, depends on C++11 compatible compiler or Boost library. I made this standalone Kaguya pack, so this lua binding can work on Windows with VS2010 without configuring Boost. (Though I've already got one.)

This two examples are extremely friendly, you can git clone it, cmake, build without hurdle. (If you come to any problems, please tell me. 😄)

HOWTO: Qt4 & VS2015

Qt4 doesn't provide an official vs2015 prebuild, and you can't easily compile one yourself.

But it is possible. See http://whudoc.qiniudn.com/2016/vs2015-qt4-playground.zip (14 MB).

14 MB = 13.6 MB of qt4 prebuild (release version only, a tiny qt4!) + 1 demo qt project.

The README.txt inside is written in chinese, steps are:

  1. unzip qt4-vs2015x64.7z in current dir;
  2. open CMD: hold Shift, right click, then hit w, Enter;
  3. set environment varible: type in CMD "set PATH=%CD%\qt4-vs2015x64\bin;%PATH%"; (better add this path to %PATH%, it's needed at runtime too.)
  4. open cmake-gui: type in CMD "cmake-gui demo";
  5. build vs2015 project: 1) set build dir; 2) set compilation option to VS2015 WIN64; 3) configure, build;
  6. use vs2015 to open .sln file in build dir, build the release version (not debug!).

You should be able to build & run this qt4 project inside VS2015.

See more at qt4-vs2015x64/README.md at master · district10/qt4-vs2015x64.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.