
- Cmake library path how to#
- Cmake library path portable#
- Cmake library path software#
- Cmake library path code#
This can be any valid CMake name, and the filename for the compiled library will be that value in your build directory. add_library(libraryNameįirstly, the first parameter to add_library is the name of the library.
Cmake library path code#
CMake’s function for creating a library is add_library, and the code block below shows the usage. Without further ado, we finally get to the interesting part. Creating Libraries With CMake’s add_library
Cmake library path portable#
This is where CMake comes to the rescue: it lets us define our libraries at a higher level, and your project will be way more portable as it should work in any supported platform.

o)? You would need to write other Makefiles to work with different environments! lib) for Windows, where they aren’t quite archives of object files (. However, what happens if you switch the compiler to clang or msvc? What happens if you want to create libraries (. Gcc -c $< -o boring_lib1.o boring_lib2.oĪr rcs cool_library.a boring_lib1.o boring_lib2.oīoring_lib2.o: boring_header2.h boring_source2.cpp boring_source3.cppĪs you can see, the above scripts should work fairly well if you want to compile your project with gcc on a Unix-like environment. In addition, any other commands needed to combine library files, move them or link them into your executable would also need to be added somewhere in the Makefile.įor example, the following Makefile uses the gcc compiler to create an executable program called cool_program, created from the source files main.c and uses the library cool_library.a. Therefore, the exact compiler flags would need to be hardcoded somewhere when creating a library file.
Cmake library path how to#
These scripts told computers how to compile projects, which executables to generate, and which libraries to create. CMake’s add_library Helps With Portabilityīefore CMake, there was a time when people wrote massive scripts for building C/C++ projects – Makefiles. Do you have certain bits of code that need to be shared with different programs/executables? Do you want to distribute some code publicly so other people can use it in their projects? Is your codebase getting ridiculously big, and you need to split parts of it into reasonable modules? Then you need to start thinking about libraries. In summary, you may need to create libraries for a few reasons. Unsurprisingly, libraries aren’t unique to C/C++!
Cmake library path software#
Interestingly, if you develop software of any kind, you are probably using various different libraries. The Problem – Why Do We Need To Create Libraries?Īccording to Wikipedia, a library is “a collection of resources by computer programs” in software development, and they include “documentation, subroutines, classes, and values” that aid developers in implementing logic for their software. In this post, we will learn how to create libraries with CMake, the types of libraries you can create and how to link them to other targets.

Perhaps you can name this library “engine”, which will be linked against your game executable for drawing in 2D. Learn how to create different library types with CMake, and how to include them in your executables!įor example, if you are developing a 2D game such as Conway’s Game Of Life, you may want to split all the structures and functions related to graphics into an independent library.

This can all be achieved with CMake’s add_library(.) function. Libraries are very useful when a C++ project becomes large enough, we may want to split the code into multiple library and executable CMake targets in order to make our project more modular and understandable.
