当前位置:网站首页>Cmake Learning Series I

Cmake Learning Series I

2022-06-13 08:55:00 Human high quality Algorithm Engineer

lately , Encountered a very serious problem , The output of the model is not aligned , The output of the compiled model is different from that of colleagues , It's the same model , The same picture , What's the problem , It turns out that I always make install, Didn't put build Delete , Colleagues explain changes to model files ,make install Yes, there is bug Of , Each time the code changes will be scanned and compiled , There is no reading into the model , So it has always been the result of the previous model , this … Looking for mistakes all morning , Is this , hold build Delete it and rearrange it .. in the final analysis , I am right cmake Not familiar …
 Insert picture description here
I hope this series will have a bright future 234

Let's get to the point

One . What is? cmake

You may have heard of several Make Tools ( Actually, I haven't heard of ), for example GNU Make ,QT Of qmake , Microsoft MS nmake,BSD Make(pmake),Makepp, wait . these Make Tools follow different norms and standards , Executed Makefile The format is also very different . This brings up a serious problem : If software wants to cross platform , You have to be able to compile on different platforms . And if you use the above Make Tools , You have to write for each standard once Makefile , It's going to be a crazy job .

CMake It is a tool designed to solve the above problems : It starts with Allow developers to write a platform independent CMakeList.txt File to customize the entire compilation process , Then further generate the required localization according to the target user's platform Makefile And engineering documents , Such as Unix Of Makefile or Windows Of Visual Studio engineering . So that “Write once, run everywhere”. obviously ,CMake It's more than the above make More advanced compilation configuration tool . Some uses CMake Well known open source projects as project architecture systems are VTK、ITK、KDE、OpenCV、OSG etc. [1].
stay linux Use under platform CMake Generate Makefile And the process of compiling is as follows :

  1. To write CMake The configuration file CMakeLists.txt
  2. Carry out orders cmake PATH perhaps ccmake PATH Generate Makefile(ccmake and cmake The difference is that the former provides an interactive interface ), among PATH yes CMakeLists.txt directory
  3. Use make Command to compile

example
Calculate the exponential power of a number

#include <stdio.h>
#include <stdlib.h>

/** * power - Calculate the power of number. * @param base: Base value. * @param exponent: Exponent value. * * @return base raised to the power exponent. */
double power(double base, int exponent)
{
    
	int result = base;
	int i;

	if (exponent == 0) {
    
		return 1;
	}

	for (i = 1; i < exponent; ++i) {
    
		result = result * base;
	}

	return result;
}

int main(int argc, char *argv[])
{
    
	if (argc < 3) {
    
		printf("Usage: %s base exponent \n", argv[0]);
		return 1;
	}
	double base = atof(argv[1]);
	int exponent = atoi(argv[2]);
	double result = power(base, exponent);
	printf("%g ^ %d is %g\n", base, exponent, result);
	system("pause");
	return 0;
}

To write CMakeLists.txt, Keep in touch with main.cpp Source files in the same directory

#  Project information 
project(zz0225)

# Specify the build target 
add_executable(Demo main.cpp)

add_executable: take main.cpp The source file is compiled into a file named Demo.exe The executable of
Build a new one under the project build Catalog
cmake After compilation , The executable file is obtained
 Insert picture description here
Right click the property page of the project , Change the working directory to OutDir, In this way, a pile of junk generated by software will be in build In the folder
Enter into exe directory , Just run directly
 Insert picture description here

原网站

版权声明
本文为[Human high quality Algorithm Engineer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270536288984.html