cmake学习

2022/3/28 6:22:53

本文主要是介绍cmake学习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!




demo文件夹下
1、build文件夹
2、src文件夹
3、SwapInt文件夹
4、CMakeLists.txt


CMakeLists.txt 内容为:
cmake_minimum_required(VERSION 3.0)

project(demo)

add_subdirectory(./SwapInt)

add_subdirectory(./src)

  

src文件夹下有main.cpp和CMakeLists.txt

main.cpp

#include <iostream>
#include "SwapInt.h"

using namespace std;

int main()
{
	int a = 10;
    int b =20;

	SwapInt(a, b);

	cout<<"a = " << a <<", b = "<< b <<endl;
}




CMakeLists.txt



include_directories(${PROJECT_SOURCE_DIR}/SwapInt)

set(EXECUTABLES(${project_binary_dir}/bin))

aux_source_directory(./ DIR_SRCS)

add_executable(demo4 ${DIR_SRCS})

target_link_libraries(demo4 SwapInt)

  

SwapInt文件夹下有 SwapInt.h  SwapInt.cpp  CMakeLists.txt

SwapInt.h

#ifndef SWAP_INT_H
#define SWAP_INT_H

void SwapInt(int& a, int& b);

#endif


 SwapInt.cpp 
#include "SwapInt.h"
void SwapInt(int&a, int& b)
{
    int t;
    t = a;
    a = b;
    b = t;
}


 CMakeLists.txt


aux_source_directory(. DIR_LIB_SRCS)

set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

add_library(SwapInt SHARED ${DIR_LIB_SRCS})

 

 

 

到build文件夹下

cmake ..

make



这篇关于cmake学习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程