Difference between revisions of "Advanced CMake Features"

From wiki.emacinc.com
Jump to: navigation, search
Line 28: Line 28:
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
{{:Templateimpl:using | initials=KY | title=Advanced CMake Features | desc=Advanced features of the CMake build system for use with the EMAC SDK. | project=OE 5.0 }}
 
{{:Templateimpl:using | initials=KY | title=Advanced CMake Features | desc=Advanced features of the CMake build system for use with the EMAC SDK. | project=OE 5.0 }}
 +
===Adding External Library Files===
 +
To add an external library to the CMake project, more changes will need to be made to the <code>CMakeLists.txt</code> file. Please refer to the code sample below. In this example, the ''ImageMagick'' library will be linked into the project.
 +
#The library directory must be included using the <code>INCLUDE_DIRECTORIES()</code> function. This function must be before the <code>ADD_EXECUTABLE()</code> function.
 +
#Use the <code>TARGET_LINK_LIBRARIES()</code> function to link the desired library to the target binary. This function must be after the <code>ADD_EXECUTABLE()</code> function.
 +
 +
<syntaxhighlight lang="cmake">
 +
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} "/usr/include/ImageMagick")
 +
 +
ADD_EXECUTABLE(main main.cpp) # This should be here by default
 +
 +
TARGET_LINK_LIBRARIES(main MagickWand)
 +
</syntaxhighlight>
 +
 +
===Adding Additional C/C++ Sourrce Files as Libraries===
 +
Adding additional source files is accomplished similarly to adding external library files. See [[#Adding_External_Library_Files | above]].
 +
# All included files must be included in the <code>SOURCES</code> list.
 +
# All header files must be included in the <code>HEADER_FILES</code> list.
 +
# Each C/C++ source file must be added as a library before adding the executable.
 +
# Add the executable, including the <code>${HEADER_FILES}</code>.
 +
# Link the target libraries to the executable.
 +
 +
<syntaxhighlight lang="cmake">
 +
SET(SOURCES
 +
    ${PROJECT_SOURCE_DIR}/include/tools.cpp
 +
    ${PROJECT_SOURCE_DIR}/include/funcs.cpp
 +
    ${PROJECT_SOURCE_DIR}/include/tools.h
 +
    ${PROJECT_SOURCE_DIR}/include/funcs.h
 +
    )
 +
 +
 +
SET(HEADER_FILES
 +
    ${PROJECT_SOURCE_DIR}/include/tools.h
 +
    ${PROJECT_SOURCE_DIR}/include/funcs.h
 +
    )
 +
 +
 +
ADD_LIBRARY(tools include/tools.cpp ${HEADER_FILES})
 +
ADD_LIBRARY(funcs include/funcs.cpp ${HEADER_FILES})
 +
ADD_EXECUTABLE(main main.cpp ${HEADER_FILES})
 +
 +
 +
TARGET_LINK_LIBRARIES(main tools funcs)
 +
</syntaxhighlight>
 +
 +
<!--{{warning | How to set up a project so that the user may build both a desktop and a target version simultaneously should be described, for cases where both exist.  For example, the md5 tool I'm working on now is just such a project.  The desktop tool will add a few features which won't be in the board-only tool, so the build system will have to set #defines I'll be able to use to selectively compile portions of the code. }}-->
  
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
Line 38: Line 83:
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
{{:Templateimpl:conclusion | initials=KY | title=Advanced CMake Features | desc=Advanced features of the CMake build system for use with the EMAC SDK. | project=OE 5.0 }}
 
{{:Templateimpl:conclusion | initials=KY | title=Advanced CMake Features | desc=Advanced features of the CMake build system for use with the EMAC SDK. | project=OE 5.0 }}
 
+
As stated previously, this guide is just a small list of common task requests. Please refer to official or other third-party documentation for more information.
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /*********************************************************************************************************/ -->
 
<!-- /******************************************  More Information  *****************************************/ -->
 
<!-- /******************************************  More Information  *****************************************/ -->

Revision as of 17:17, 14 September 2015

TODO: {{#todo: InProgress (09.14.2015-11:59->KY+)|Klint Youngmeyer|OE 5.0,KY}}

There are several advanced features of the CMake build system that may be of use on projects as they get larger. This is far from a comprehensive list, and information related to unlisted tasks may be found on official CMake documentation.

Background

This page is written with the assumption that the project being worked on has been created using the oe_init_project script or using the EMAC New C/C++ Project in Qt Creator.

General Information

Advanced CMake Features

Adding External Library Files

To add an external library to the CMake project, more changes will need to be made to the CMakeLists.txt file. Please refer to the code sample below. In this example, the ImageMagick library will be linked into the project.

  1. The library directory must be included using the INCLUDE_DIRECTORIES() function. This function must be before the ADD_EXECUTABLE() function.
  2. Use the TARGET_LINK_LIBRARIES() function to link the desired library to the target binary. This function must be after the ADD_EXECUTABLE() function.
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} "/usr/include/ImageMagick")

ADD_EXECUTABLE(main main.cpp) # This should be here by default

TARGET_LINK_LIBRARIES(main MagickWand)

Adding Additional C/C++ Sourrce Files as Libraries

Adding additional source files is accomplished similarly to adding external library files. See above.

  1. All included files must be included in the SOURCES list.
  2. All header files must be included in the HEADER_FILES list.
  3. Each C/C++ source file must be added as a library before adding the executable.
  4. Add the executable, including the ${HEADER_FILES}.
  5. Link the target libraries to the executable.
SET(SOURCES 
    ${PROJECT_SOURCE_DIR}/include/tools.cpp
    ${PROJECT_SOURCE_DIR}/include/funcs.cpp 
    ${PROJECT_SOURCE_DIR}/include/tools.h
    ${PROJECT_SOURCE_DIR}/include/funcs.h
    )


SET(HEADER_FILES
    ${PROJECT_SOURCE_DIR}/include/tools.h
    ${PROJECT_SOURCE_DIR}/include/funcs.h
    )


ADD_LIBRARY(tools include/tools.cpp ${HEADER_FILES})
ADD_LIBRARY(funcs include/funcs.cpp ${HEADER_FILES})
ADD_EXECUTABLE(main main.cpp ${HEADER_FILES})


TARGET_LINK_LIBRARIES(main tools funcs)


Examples

Conclusion

As stated previously, this guide is just a small list of common task requests. Please refer to official or other third-party documentation for more information.

Further Information

Where to Go Next
Pages with Related Content