0% found this document useful (0 votes)
18 views5 pages

C-Lion Library Construction Usage Guide V2

The document outlines the steps to create and utilize a static library in C-Lion, including project setup, modifying the CMakeLists.txt file, and building the project. It emphasizes the importance of using the add_library() command for static libraries instead of add_executable() and provides instructions for linking the library in another project. Additionally, it details the necessary changes to the CMakeLists.txt file for proper integration of the library in the consuming project.

Uploaded by

sibel.celen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

C-Lion Library Construction Usage Guide V2

The document outlines the steps to create and utilize a static library in C-Lion, including project setup, modifying the CMakeLists.txt file, and building the project. It emphasizes the importance of using the add_library() command for static libraries instead of add_executable() and provides instructions for linking the library in another project. Additionally, it details the necessary changes to the CMakeLists.txt file for proper integration of the library in the consuming project.

Uploaded by

sibel.celen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Static Library Construction:

1. Create a project. Add your .cpp/.h files to your project folder. Here, staticlib.cpp contains
library function implementations and staticlib.h is the header file that contains the
functions' prototypes. It should look as follows after adding your files to your project:

(Alternatively, you may generate a new static library project and write your own
code inside the header and cpp files automatically created by C-Lion; if you do so,
you do need the second step since CMakeLists.txt file is automatically configured)

2. Now, to construct a library, changes need to be made to the “CmakeLists.txt” file. By


default, when C-Lion asks you to add a .cpp/.h file into “CmakeLists.txt” file, it will add
inside the add_executable() configuration. For executable programs (where the output is
runnable .exe file) this is the correct action to do. However, a library is NOT an executable
file (.exe), therefore we do not do that. To create a static library file (the extension for
static library is .a in C-Lion), simply use the command add_library() instead. Another
important thing here is that we do not any cpp file with main function to add_library(); we
add only the cpp file containing function implementations of the library. You may also add
the corresponding header file to add_library, but this is not needed. You can see the
example for this project (our name in this example is Staticlib_CL) below:
3. Since you have changed CMake configuration, you have to click on "Sync CMake Changes".
4. Build the project via Menu (Build -> Build Project). Do not run it since there is nothing to
run in this project (well, if you run it since building will be done before, it also works but
you get an unnecessary error message). After building the project with these
configurations, the library file can be found in “cmake-build-debug” folder (or “cmake-
build- release” if you opted to run the project in release configuration). The extension for
the library file is “.a”.
Utilizing a Static Library in a Program:
1. Go to the project where the library will be used.
2. Create a new directory inside your project (we named it as "lib"). Example can be seen
below (you can also use your operating system tools such as explorer):
3. Now put the library file (with the extension “.a”) into the created directory. Example for a
directory named “lib” is given below (you can copy paste the .a file for this purpose):

4. Now, modify the “CmakeLists.txt” file as follows (explanations are below).

 Green: Your project name


 Red: Path (Full Path Name) to the directory you have created in step 2. The example
shows for a directory created with the name “lib”. You can copy the path as follows.
Select your directory and then right click. Choose the options highlighted below:
After copying the path, paste it as an argument to the “link_directories()” command
of CMakeLists.txt file.

IMPORTANT: This will give you the path with “\” symbols in
Windows. You have to change these to “/” like in the example
shown above (First image in step 4).
 Blue: The name of the library file without the “lib” prefix and “.a” suffix. In the
example, the library file is named as “libStaticlib_CL.a” which will be written here as
“Staticlib_CL” like in the example.
 Yellow: Name of the .cpp file that will be compiled when building the project. This is
the one that uses the library functions. You may also add the library's header file
here as well but not needed.

5. Sync changes for CMake and then run your program.

You might also like