You can link libraries into the executables the same way you can link libraries to other libraries. They require that there is a main function that can be run or else the build will fail. ExecutablesĮxecutable targets serve only the purpose of creating a file that could be run by the operating system and execute a set of commands then finish. # You would still need the include files also. # if we consider that the lib file is in the current directory. # target_link_libraries(libfolder2_static PUBLIC libfolder_static.lib) Target_link_libraries(libfolder2_static PUBLIC libfolder_static ) The other way would be to link the libfolder.lib file directly: project(libfolder2 LANGUAGES C CXX)Īdd_library(libfolder2_static STATIC include/B.h src/B.cpp) If the other library is part of your whole CMake project structure you can do so using the target name (for example if you want to create a new library called libfolder2 you would link libfolder_static into it. It is pretty easy to create the library right? You can also link against other libraries that were created. Target_include_directories(libfolder_static # libfolder_static would also have those includes # Also because we set that to public any other target that links to # This means that A.cpp can include A.h by writing # This will set include directories for this target # add_library(libfolder_dynamic SHARED include/A.h src/A.cpp) # libfolder_static is called a target name and can be used laterĪdd_library(libfolder_static STATIC include/A.h src/A.cpp) It is best that the definitions of functions are written in the include files and that they are easily used in another project. This is to make it easier to later copy the header directory because to use a library you would usually need to specify the same function signatures in another project. You will notice that the headers and source files are split. Lets start with my usual project strcuture for a library: My recomendation for game development is to use dynamic linking during your debug process (debug builds) as you would want quicker build times and static linking when creating a release build. Usual extension for these libraries would be: (. The dynamic library will be evaluated at run time and usually needs to be in the same directory as the executable. It will produce a smaller executable file and will have a faster build time. The static library will be used in the linking process. This takes more time during the build of the executable and produces a larger executable but in the long run the application will run faster.ĭynamic libraries produce two files – a static library that is hollow and only has references and a dynamic library that should be copied along the executable. Static linking means that the contents of the lib file will be compiled into the end executable. Static libraries produce a single file that you can later link into an executable ( read more on compile and link). I am aware that the choice of dynamic linking is only available on Windows but it is useful for everyone to know this. ![]() When creating a library you have two choices – to make it static or dynamic. Later if you work on another project you could be able to separate those chunks and take the ones that you would need and are reusable. It is way easier to configure unit/integration testing on a separate library than on an executable.Īnother main benefit is that you split your code into meaningful chunks. One of these benefits is the ability to test your code. ![]() In C/C++ there are other benefits to write your code in a library. Using libraries saves you a great deal of time and effort to learn how to make certain things. In the internet you can find many libraries for your projects that are maintained by other people. Libraries contain code that you write and you would like to share between multiple projects. Lets start with library as it is the building block for executables. There are two main types of targets for your C/C++ project. I have a good tutorial on this site on how to manage your projects with CMake. Basic knowledge on how to work with CMake. ![]() You can start with my begginer article on C++. This article requires that you have some basic knowledge of: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |