Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the library file and header file link in CMake compilation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the CMake compilation of the library file and header file link is what the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that you read this CMake compilation of the library file and header file link is what the article will have a harvest, let's take a look at it.

External library file, header file link instruction header file search path INCLUDE_DIRECTORIES ([AFTER | BEFORE] [SYSTEM] dir1 dir2.)

[AFTER | BEFORE]: add method. Default

AFTER AFTER: append content at the back (new content is added later)

BEFORE: add content at the front (new content comes first)

Dir: the path address of the header file. Paths containing spaces are enclosed in double quotation marks.

Link library file LINK_DIRECTORIES (directory1 directory2...)

Directory: library file name

This directive is used to add non-standard shared library search paths.

TARGET_LINK_LIBRARIES (target library1 library2...)

Target: linked object library: library name

Example

Here we use the previously generated installed library file libhello.so to demonstrate.

New engineering space

First, create a new project file and define the project CMakeLists.txt file.

# New Project Space t4mkdir-p ~ / cmake_test/t4cd ~ / cmake_test/t4# New subdirectory srcmkdir src# New Project CMakeLists.txtvim CMakeLists.txt

The subdirectory src is used to store the source code. The CMakeLists.txt content in the project root directory is as follows:

# Project name PROJECT (NEWHELLO) # add source directory ADD_SUBDIRECTORY (src) to define src content

Enter the subdirectory src and create a new source file main.cpp

# enter the src subdirectory cd ~ / cmake_test/t4/src# new source file vim main.cpp

The main.cpp file is as follows:

/ / main.cpp#include using namespace std;int main () {HelloFunc (); return 0;}

The previously encapsulated HelloFunc function is called in the source file, and its header file and link should be defined. Create a new CMakeLists.txt in the src directory as follows:

# add header file search path INCLUDE_DIRECTORIES (/ usr/include/hello) # add link TARGET_LINK_LIBRARIES (main libhello.so) # generate target binary ADD_EXECUTABLE (main main.cpp)

In the above statement, link the dynamic library libhello.so with main, and add the header file to the search path. Alternatively, you can choose to link the static library libhello.a by modifying it as follows:

# add link TARGET_LINK_LIBRARIES (main libhello.a) compilation

Create a new build subdirectory in the T4 root directory for compilation:

# create a new build subdirectory cd ~ / cmake_test/t4mkdir build & cd build# build cmake.. # compile make-j12

At this point, the executable binary file main will be generated in build/src, and the effect of running the program is as follows:

# run program cd ~ / cmake_test/t4/build/srcbash main# program output: this is the end of Hello World's article on "what are library files and header file links in CMake compilation?" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the library file and header file link in CMake compilation". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report