In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use CMake under Linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
CMake is a cross-platform installation (compilation) tool that can describe the installation (compilation process) of all platforms with simple statements. He can output a variety of makefile or project files, and can test the C++ features supported by the compiler, similar to automake under UNIX.
I. single file directory
1. Edit the C program file, named main.c
# include int main (void) {printf ("Hello World.\ n"); return 0;}
two。 Write a CMakeLists.txt file and save it in the same path as main.c
# Minimum required CMake Versioncmake_minimum_required (VERSION 3.6.1) # Project Nameproject (hello) # change the current directory (.) Add the variable SRC_LISTAUX_SOURCE_DIRECTORY (. SRC_LIST) # generate application hello (generate hello.exe under windows) ADD_EXECUTABLE (hello ${SRC_LIST})
3. Run the cmake command to generate MakeFile, and then run the make command to generate the hello executable (to prevent file clutter, set up a build directory, where you can run the cmake command)
Mgh@mgh-OptiPlex-5050:~/ Desktop / cmake_test/test2/build$ cmake.-- The C compiler identification is GNU 5.4.0muri-The CXX compiler identification is GNU 5.4.0Murray-Check for working C compiler: / 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: / usr/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- done-- Configuring done-- Generating done-- Build files have been written to: / home/mgh/ Desktop / cmake_test/test2/buildmgh@mgh-OptiPlex-5050:~/ Desktop / cmake_test/test2/build$ makeScanning dependencies of target hello [50%] Building C object CMakeFiles / hello.dir/test1.c.o [100%] Linking C executable hello [100%] Built target hellomgh@mgh-OptiPlex-5050:~/ desktop / cmake_test/test2/build$. / helloHello World.
2. Multi-file directory
1. Directory structure
Mgh@mgh-OptiPlex-5050:~/ Desktop / cmake_test$ tree testtest ├── CMakeLists.txt ├── print │ ├── CMakeLists.txt │ ├── print.c │ └── print.h └── test.c
two。 Edit C program file
Test.c
# include # include "print/print.h" int main (void) {print (); return 0;}
Print.h
# ifndef PRINT_H#define PRINT_Hextern void print (); # endif
Print.c
# include extern void print () {printf ("Hello World.\ n");}
3. Edit the CMakeLists.txt file
In this multi-directory situation, you need to write CMakeLists.txt files separately in each source file path, and for this example, you need to write CMakeLists.txt files in the test root directory and the print directory.
For convenience, we can compile the files in the print directory into static libraries and then call them by the main function.
CMakeLists.txt file in the test directory:
# Minimum required CMake Versioncmake_minimum_required (VERSION 3.6.1) # Project Nameproject (hello) # all source files under the current directory are saved to SRC_LIST AUX_SOURCE_DIRECTORY (. SRC_LIST) # add print subdirectory add_subdirectory (print) # specify generation target ADD_EXECUTABLE (hello ${SRC_LIST}) # add link library target_link_libraries (hello printFunc)
CMakeLists.txt file in the print directory:
# all source files under the current directory are saved to AUX_SOURCE_DIRECTORY (. SRC_LIB) # generate link library ADD_LIBRARY (printFunc ${SRC_LIB})
4. Compile and run
Mgh@mgh-OptiPlex-5050:~/ Desktop / cmake_test/test/build$ cmake.-- The C compiler identification is GNU 5.4.0muri-The CXX compiler identification is GNU 5.4.0Murray-Check for working C compiler: / 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: / usr/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- done-- Configuring done-- Generating done-- Build files have been written to: / home/mgh/ Desktop / cmake_test/test/buildmgh@mgh-OptiPlex-5050:~/ Desktop / cmake_test/test/build$ makeScanning dependencies of target printFunc [25] Building C object print / CMakeFiles/printFunc.dir/print.c.o [50%] Linking C static library libprintFunc.a [50%] Built target printFuncScanning dependencies of target hello [75] Building C object CMakeFiles/hello.dir/test.c.o [100%] Linking C executable hello [100%] Built target hellomgh@mgh-OptiPlex-5050:~/ Desktop / cmake_test/test/build$. / helloHello World.
The above is all the contents of the article "how to use CMake under Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.