In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use the automated construction system CMake", the content is simple and clear, and I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to use the automated construction system CMake".
CMake is a cross-platform automated build system that uses a file called CMakeLists.txt to describe the build process and can produce standard build files such as Unix's Makefile or Windows Visual C++ 's projects/workspaces.
Example 1:Hello World
There is only one file HelloWorld.cpp in the source code.
# includeint main (int argc, char * argv []) {std::cout "Hello World!" Return 0;} 123456
CMakeLists.txt is only three lines long (the process of using cmake to manage a project, that is, the process of writing CMakeLists.txt)
Cmake_minimum_required (VERSION 2.8.9) project (hello) add_executable (hello helloworld.cpp) 123
The first line is used to specify the minimum version of cmake. The second line specifies the project name (this name is arbitrary). The third line specifies to compile an executable file, hello is the first parameter, indicating the file name of the generated executable file (this file name is also arbitrary), and the second parameter helloworld.cpp is used to specify the source file.
If you have cmake installed on your computer, then we are done with it. The first step is to generate the Makefile file with cmake
Note: the cmake command is followed by the directory where CMakelist.txt is located, which does not have to be the current directory, or you can create a new build directory or a directory with other names to generate build files, which is also done in the actual project, so that the code will be clean and easy for git to manage.
The second step, make compiler & & compiled successfully through the previous step, we found that several files have been added to the current directory, especially the Makefile file, the third step, the test program here, the first program managed by cmake, successful! Example 2: a project containing a directory structure
In example 1, it doesn't show any of the advantages of cmake at all, and we go around a lot of problems that can be solved with one line of Gmail +. But the original advantage of cmake is to manage huge projects. This example uses the smallest program to represent a project with a directory structure. There are source file directory and header file directory.
Cmake_minimum_required (VERSION 2.8.9) project (directory_test) # Bring the headers, such as Student.h into the projectinclude_directories (include) # Can manually add the sources using the set command as follows:#set (SOURCES src/mainapp.cpp src/Student.cpp) # However, the file (GLOB...) Allows for wildcard additions:file (GLOB SOURCES "src/*.cpp") add_executable (testStudent ${SOURCES}) 12345678910111213
Compared to the first example, CMakelist.txt has the following changes:
Use include_directories () to include the header file directory and use set (SOURCES... ) or GLOB (or GLOB_RECURSE) set the source file SOURCESadd_executable to use the variable SOURCES instead of the specific file name. The next step is the same as example 1, except that we create a new build directory to store the compilation intermediate files, as shown in the following figure: next step make, and then run the results as follows: example 3: dynamic library compilation (.so)
With the foundation of the first two examples, we only need to look at the directory structure and CMakelist.txt.
CMakelist.txt is as follows:
Project (directory_test) set (CMAKE_BUILD_TYPE Release) # Bring the headers, such as Student.h into the projectinclude_directories (include) # However, the file (GLOB...) Allows for wildcard additions:file (GLOB SOURCES "src/*.cpp") # Generate the shared library from the sourcesadd_library (testStudent SHARED ${SOURCES}) # Set the location for library installation-i.e., / usr/lib in this case# not really necessary in this example. Use "sudo make install" to applyinstall (TARGETS testStudent DESTINATION / usr/lib) 123456789101112131415
Two important changes:
Instead of using add_executable (), we use add_library () install to specify the installation directory. When sudo make install is executed, the dynamic library will be installed in the / usr/lib directory as in the previous two examples, and the cmake make compilation results are as follows: example 4: static library compilation (.a)
Based on example 3, we compile a static library
Modify the CMakeList.txt to look like this:
Cmake_minimum_required (VERSION 2.8.9) project (directory_test) set (CMAKE_BUILD_TYPE Release) # Bring the headers, such as Student.h into the projectinclude_directories (include) # However, the file (GLOB...) Allows for wildcard additions:file (GLOB SOURCES "src/*.cpp") # Generate the static library from the sourcesadd_library (testStudent STATIC ${SOURCES}) # Set the location for library installation-i.e., / usr/lib in this case# not really necessary in this example. Use "sudo make install" to applyinstall (TARGETS testStudent DESTINATION / usr/li12345678910111213141516
As you can see, you just need to change the shared in add_library to static. The compilation results are as follows:
Example 5: using static or dynamic libraries
Let's test the results of our example 3 below. The code and CMakeList.txt are as follows:
# include "Student.h" int main (int argc, char * argv []) {Student s ("Joe"); s.display (); return 0 } 1234567cmake_minimum_required (VERSION 2.8.9) project (TestLibrary) # For the shared library:set (PROJECT_LINK_LIBS libtestStudent.so) link_directories (~ / exploringBB/extras/cmake/studentlib_shared/build) # For the static library:#set (PROJECT_LINK_LIBS libtestStudent.a) # link_directories (~ / exploringBB/extras/cmake/studentlib_static/build) include_directories (~ / exploringBB/extras/cmake/studentlib_shared/include) add_executable (libtest libtest.cpp) target_link _ libraries (libtest ${PROJECT_LINK_LIBS}) 123456789101112131415
The result is as follows (the directory in CMakeList.txt should be changed according to your situation):
It worked!
The above is all the contents of the article "how to use the Automated Construction system CMake". 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.