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

How to use Cmake

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use Cmake". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Cmake.

1. Multiple source files, use the command aux_source_directory (dir var):

At the end of the last article, there was a problem, that is, when there are multiple source files in the same directory, you can't always manually add source files to the third command below. How inefficient it is:

Cmake_minimum_required (VERSION 2.8)

Project (main)

Add_executable (main main.c test1.c)

So in order to solve this inefficient operation, there is an instruction in cmake that can completely solve the problem, but to illustrate the point, I added two more files: test2.c and test2.h:

Root@txp-virtual-machine:/home/txp/test# ls

1 cmake_install.cmake main.c test1.h touch2.c

CMakeCache.txt CMakeLists.txt Makefile test2.c touch2.h

CMakeFiles main test1.c test2.h

The test2.c content is as follows:

# include

# include "test2.h"

Void func1 ()

{

Printf ("i like the cmake\ n")

}

The test2.h content is as follows:

# ifndef _ TEST2_H_

# define _ TEST2_H_

Void func1 ()

# endif

Finally, the func1 function is called in main.c:

# include

# include "test1.h"

# include "test2.h"

Int main (void)

{

Func1 ()

Func (8)

Printf ("TXP embedded\ n")

Return 0

}

Next, let's focus on the fact that we can solve the above inefficiency problem by using aux_source_directory (dir var) in cmake. Next, we can do this in CMakeLists.txt:

Cmake_minimum_required (VERSION 2.8)

Project (main)

Aux_source_directory (. SRC_LIST)

Add_executable (main ${SRC_LIST})

Then compile:

Root@txp-virtual-machine:/home/txp/test# cmake.

-- Configuring done

-- Generating done

-- Build files have been written to: / home/txp/test

Root@txp-virtual-machine:/home/txp/test# make

Scanning dependencies of target main

[25%] Building C object CMakeFiles/main.dir/main.c.o

[50%] Linking C executable main

Root@txp-virtual-machine:/home/txp/test#. / main

I like the cmake

The b is 8

TXP embedded system

Description:

Aux_source_directory (. SRC_LIST): means to add all the source files in the current directory to the source list variables, and finally use add_executable (main ${SRC_LIST}) to process all useful source files into the target file main. However, this method also has its disadvantage, which is to add all the source files in the current directory to the variable SRC_LIST. If we don't need some useless files (just get the source files we need), we can do this:

Cmake_minimum_required (VERSION 2.8)

Project (main)

Set (SRC_LIST

. / main.c

. / test1.c

. / test2.c

)

Add_executable (main ${SRC_LIST})

This can be compiled:

Root@txp-virtual-machine:/home/txp/test# cmake.

-- Configuring done

-- Generating done

-- Build files have been written to: / home/txp/test

Root@txp-virtual-machine:/home/txp/test# make

[100%] Built target main

2. In the above example, we will find that the source files in the same directory are messy, so there is a rule in cmake that you can put the same type and related source files in the same directory. For example, now I create two directory files, test1 and test2, under the test directory, and put test1.c, test1.h, test2.c and test2.h in these two directories at the same time:

Root@txp-virtual-machine:/home/txp/test# mkdir-p test1 test2

Root@txp-virtual-machine:/home/txp/test# ls

@ CMakeFiles main test1 test2

1 cmake_install.cmake main.c test1.c test2.c

CMakeCache.txt CMakeLists.txt Makefile test1.h test2.h

Then put the relevant files into these two directory files:

Root@txp-virtual-machine:/home/txp/test# mv test1.c test1.h test1

Root@txp-virtual-machine:/home/txp/test# mv test2.c test2.h test2

Root@txp-virtual-machine:/home/txp/test# ls

@ CMakeCache.txt cmake_install.cmake main Makefile test2

1 CMakeFiles CMakeLists.txt main.c test1

Root@txp-virtual-machine:/home/txp/test# tree

├── cmake_install.cmake

├── CMakeLists.txt

├── main

├── main.c

├── Makefile

├── test1

│ ├── test1.c

│ └── test1.h

└── test2

├── test2.c

└── test2.h

Then it's time to modify the rule properties in CMakeLists.txt:

Cmake_minimum_required (VERSION 2.8)

Project (main)

Include_directories (test1 test2)

Aux_source_directory (test1 SRC_LIST)

Aux_source_directory (test2 SRC_LIST1)

Add_executable (main main.c ${SRC_LIST} ${SRC_LIST1})

Then compile the output, which can also be passed:

Root@txp-virtual-machine:/home/txp/test# cmake.

-- Configuring done

-- Generating done

-- Build files have been written to: / home/txp/test

Root@txp-virtual-machine:/home/txp/test# make

Scanning dependencies of target main

[25%] Building C object CMakeFiles/main.dir/main.c.o

[50%] Building C object CMakeFiles/main.dir/test1/test1.c.o

[75] Building C object CMakeFiles/main.dir/test2/test2.c.o

[100%] Linking C executable main

[100%] Built target main

Root@txp-virtual-machine:/home/txp/test# ls

@ CMakeCache.txt cmake_install.cmake main Makefile test2

1 CMakeFiles CMakeLists.txt main.c test1

Root@txp-virtual-machine:/home/txp/test#. / main

I like the cmake

The b is 8

TXP embedded system

Thank you for your reading, the above is the content of "how to use Cmake", after the study of this article, I believe you have a deeper understanding of how to use Cmake, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report