Using a third-party library when compiling a C language project using CMake

Source: Internet
Author: User
Tags gtk

About the installation of CMake, CMakeLists.txt grammar rules, CMakeLists.txt various configuration options and other complex and professional knowledge, limited to my ability, here no longer explain

Demo using glibc2.0 Library

A project directory


[Email protected]:~/code/cmake/eclipse/hellocmake$ tree.├──bin├──cmakelists.txt├──include│├──hello.h│└──sum.h├─ ─LIB└──SRC    ├──cmakelists.txt    ├──hello    │├──cmakelists.txt    │└──hello.c    ├──main    │├── CMakeLists.txt    │└──main.c    └──sum        ├──cmakelists.txt        └──sum.c7 directories, Files[email protecte d]:~/code/cmake/eclipse/hellocmake$



In Hello,main,sum three subdirectories, except Hello, other directories are not used to third-party libraries, you can see here and there is no third-party library, how to use CMake. In fact, this example adds a Hello directory based on my previous "CMake" blog post, where the GLIBC library is used. HELLO.C's code is from: http://blog.chinaunix.net/uid-25696269-id-466217.html



Two source codeInclude/sum.h


/* * sum.h * *  Created on:2015 August 11 *      Author:laolang */#ifndef include_sum_h_#define include_sum_h_int sum (int A, int b); #endif/* Include_sum_h_ */
Include/hello.h



/* * hello.h * *  Created on:2015 August 11 *      Author:laolang */#ifndef include_hello_h_#define include_hello_h_void H Ello (); #endif/* Include_hello_h_ */




Main/main.c


#include <stdio.h> #include ". /.. /include/sum.h "#include". /.. /include/hello.h "int main (void) {int a = 0;int B = 0;puts (" Please enter two integers: "), scanf ("%d%d ", &a,&b);p rintf ("%d +%d =%d\n ", A,b,sum (A, b)); hello ();p rintf ("\n\nhello world!\n"); return 0;}



Sum/sum.c


#include ". /.. /include/sum.h "int sum (int a, int b) {return a + B;}




Hello/hello.c


#include <stdio.h> #include <glib.h> #include ".. /.. /include/hello.h "void Hello () {g_printf (" \n\nhello world! From glibc2.0\n ");}




Three CMakeLists.txtTop Floor CMakeLists.txt


# Top level cmakelists.txt# Definition Project name Project (HELLO) # define subdirectory src, use recursive call to SRC in makelists.txtadd_subdirectory (SRC)


Src/cmakelists.txt


# src cmakelists.txt# definition subdirectory add_subdirectory (main) #定义子目录add_subdirectory (sum) add_subdirectory (hello)



Src/main/cmakelists.txt


# main cmakelists.txt# source file list set (Src_list main.c) # header file List include_directories (${hello_source_dir}/include) # Sets the path of the generated executable file set (Executable_output_path ${hello_source_dir}/bin) # generated executable add_executable (Test ${src_list}) # Required library file directory Link_directories (${hello_source_dir}/lib) # requires a linked library file target_link_libraries (test sum HELLO)



Src/sum/cmakelists.txt


# sum cmakelists.txt# set compiler set (Cmake_c_compiler gcc) # source file list set (Src_list sum.c) # header file directory include_directories (${hello_ Source_dir}/include) # Set the path of the generated library file set (Library_output_path ${hello_source_dir}/lib) # Generated library file add_library (sum STATIC ${ Src_list})



Src/hello/cmakelists.txt


# Hello CMakeLists.txt # Set compiler set (Cmake_c_compiler gcc) # source file list set (Src_list hello.c) # header file directory include_directories (${hello_ Source_dir}/include) # Set the path to the generated library file set (Library_output_path ${hello_source_dir}/lib) # The Directory of required glibc (GTK) library files Include_ Directories (/usr/include/glib-2.0/usr/lib/x86_64-linux-gnu/glib-2.0/include) # Generated library file add_library (Hello STATIC ${ Src_list}) # requires a linked glibc (GTK) library file target_link_libraries (Hello glib-2.0)



Four CMake && make


[email protected]:~/code/cmake/eclipse/hellocmake$ tree.├──bin├──cmakelists.txt├──include│  ├── Hello.h│  └──sum.h├──lib└──src├──cmakelists.txt├──hello│  ├──cmakelists.txt│&n Bsp; └──hello.c├──main│  ├──cmakelists.txt│  └──main.c└──sum├──c Makelists.txt└──sum.c7 directories, files[email protected]:~/code/cmake/eclipse/hellocmake$ mkdir build[e mail protected]:~/code/cmake/eclipse/hellocmake$ CD build/[email protected]:~/code/cmake/eclipse/ hellocmake/build$ CMake. --The C compiler identification is GNU 4.8.4--The CXX compiler identification are GNU 4.8.4--Check for working C compile  R:/usr/bin/cc--Check for working C compiler:/USR/BIN/CC--works--detecting C compiler ABI info--detecting C compiler ABI info-done--detecting C compile features--detecting C compile features-done--Check for working CXX compiler:/u sr/bin/c++--Check For working CXX compiler:/usr/bin/c++--works--detecting CXX compiler ABI info--detecting CXX compiler Abi info-done- -Detecting CXX Compile features--detecting CXX compile Features-donecmake Warning (dev) at src/main/cmakelists.txt:13  (add_executable): Policy CMP0037 is not set:target names should isn't be reserved and should match a validity pattern.  Run "CMake--help-policy CMP0037" for policy details.  Use the Cmake_policy command to set the policy and suppress this warning.  The target name "test" is reserved or not valid for certain CMake features, such as generator expressions, and may result In undefined behavior.  This warning are for project developers. Use-wno-dev to suppress it.  CMake Warning (Dev) in CMakeLists.txt:No cmake_minimum_required command is present.  A line of code such as cmake_minimum_required (VERSION 3.3) should is added at the top of the file. The version specified may is lower if you wish to support older CMake versions for this project. For more information run "CMake--help-policy CMP0000".  This warning are for project developers.  Use-wno-dev to suppress it.--configuring Donecmake Warning (Dev) at Src/main/cmakelists.txt:13 (add_executable): Policy  CMP0003 should be set before the line. ADD code such as if (Command Cmake_policy) cmake_policy (SET CMP0003 NEW) endif (command Cmake_policy) as early a  s possible but after the most recent call to Cmake_minimum_required or Cmake_policy (VERSION). This warning appears because target "test" links to some libraries for which the linker must search:glib-2.0 and OT Her libraries with known full path:/HOME/LAOLANG/CODE/CMAKE/ECLIPSE/HELLOCMAKE/LIB/LIBSUM.A CMake is adding Directori Es in the second list to the linker search path in case they is needed to find libraries from the first list (for Backwa  RDS compatibility with CMake 2.4).  Set policy CMP0003 to the or NEW to enable or disable this behavior explicitly. Run "CMake--help-policy CMP0003" for more information.  This warning are for project developers. Use-wno-dev to suppress it.--generating done--Build files has been written to:/home/laolang/code/cmake/eclipse/helloc make/build[email protected]:~/code/cmake/eclipse/hellocmake/build$ makescanning dependencies of Target hello[ 16%] Building C object src/hello/cmakefiles/hello.dir/hello.o[33%] linking C static library. /.. /.. /lib/libhello.a[33%] Built target helloscanning dependencies of target sum[50%] Building C Object src/sum/cmakefiles/sum . dir/sum.o[66%] linking C Static library. /.. /.. /lib/libsum.a[66%] Built target sumscanning dependencies of target test[83%] Building C object src/main/cmakefiles/test. dir/main.o[100%] Linking C executable. /.. /.. /bin/test[100%] Built target test[email protected]:~/code/cmake/eclipse/hellocmake/build$ CD. [email protected]:~/code/cmake/eclipse/hellocmake$./bin/test Please enter two integers: 2 + 3 = 5Hello world! From Glibc2.0hello world! [Email protected]:~/code/cmake/eclipse/hellocmake$ 


51-point question

Parameter sources in Include_directories and target_link_libraries: Pkg-config--cflags glibc-2.0 pkg-config--cflags glibc-2.0

In the share/cmakex.x/modules of CMake's compressed package, there are many findxxx.cmake files, that is, CMake provides good finder about third-party libraries, so I am in this http://blog.csdn.net/ dbzhang800/article/details/6329314 Blog found the use of methods, but I did not succeed, prompted Glib.h not found. In the technical question and answer, the use of the complete absolute path, that is, using the output of Pkg-config--cflags gblic-2.0 and Pkg-config--libs gblic-2.0 respectively as Include_ Parameters of directories and target_link_libraries. Although this can solve the problem, but this is certainly a problem, you can see the output of the CMake command has a lot of warnings, for I do not want to see the warning, it is very helpless thing. There are other problems, such as the library upgrade, change the name, or even the installation path is changed, the project is not good, the project is relatively large and complex, then modify the CMakeLists.txt will be very painful things. The reason why I use CMakeLists.txt is two: 1 Fast 2 cross-platform, and I this method one is once the library changes are not fast, and the second is to change the system will change the CMakeLists.txt, this is not.

I hope the CMake master can provide an example, preferably can have cmake own finder and CMake not included third-party libraries. Thank you!










Using a third-party library when compiling a C language project using CMake

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.