CMakeTutorial.pdf - CMakeTutorial Whatisalibrary ,.The...

Info icon This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMake Tutorial What is a library? Before we get to technical details, you have already been using libraries since CS1050. The libraries such as stdio.h, stdlib.h, and string.h that provide you abilities to perform actions on your data. Every operating system uses libraries to save space by discouraging repeated solutions and providing efficient implementations to users. You don’t want to have 30 implementations of strcpy on any system do we? When we link a library we are able to use all the functions/methods in that library in our code. In order to help make libraries portable between operating system, all libraries use a build system. What is a build system? A build system makes life easier for developers and library builders when creating cross platform applications/solutions. The build system will remove the tedious labor of creating the perfect Makefile for any operating system. In this class we are using a relative newcomer to the build system world. CMake is becoming widely popular build system for it’s very easy macro system. However, knowing more than one build system is important such as Autotools. Note: CMake is creating the Makefile based on your current system, some systems have varying libraries and compilers. Building a Library with CMake CMake (CMakeList.txt) Library Build cmake_minimum_required (VERSION 2.8) project (HelloLibrary) SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ­g ­Wall" ) include_directories (${CMAKE_CURRENT_DIRECTORY}/include) add_library (HelloLibrary SHARED src/helloworld.c) add_library (HelloLibraryStatic STATIC src/helloworld.c) install (TARGETS HelloLibrary DESTINATION lib) INSTALL (FILES include/helloworld.h DESTINATION include) Building a CMake Library project: Sample Project Library Directory Layout FirstCMakeLibraryProject/ ­>build/ ­>CMakeLists.txt ­>include/ ­>helloworld.h ­>src/ ­>helloworld.c You will build your library inside of the build directory. mkdir build ​ (If you don’t already have one, we are expecting a build directory in your submission) cd build cmake ..​ (This runs the cmake command on the CMakeLists.txt file in the previous directory) make​ (Builds your helloworld .so and .a library files) sudo make install​ (Installing the library (.so) in the directory /usr/local/lib/ and installing the .h file(s) in /usr/local/include/) sudo ldconfig ​ (Updates the loader to find our new library when linked) make clean​ ( To remove all objects/library files created in build, before submission always ​ rm ­r *​ your build directory) How will I use this library I just built and installed? Just like including stdio.h, you will include the .h of your library you just created. Below is a program that will use our custom shared library. helloworldProgram.c #include <stdio.h> #include <helloworld.h> //contains the getHelloWorld and freeHelloWorld function int main (void) { char *str = getHelloWorld(); printf(“%s\n”,str); freeHelloWorld(str); return 0; } The above program needs an updated CMakeLists.txt from our current one to include our custom shared library. CMake (CMakeList.txt) cmake_minimum_required (VERSION 2.8) project (HelloWorldProgram) SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ­g ­Wall" ) add_executable (helloWorldProgram src/helloWorldProgram.c) target_link_libraries (helloWorldProgram HelloLibrary ) We now have successfully linked our program with our library. We may now build with cmake and make. Let's demo a sample library and application using cmake?! Please clone the repo from ​ ​ . It is more complex than a simple helloworld example. ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern