Cmake入门与Billards240401

本文详细展示了在Linux环境下编译C++项目的过程,包括使用g++编译器、链接gtest库、HDF5库以及自定义库,通过CMake进行配置管理。
摘要由CSDN通过智能技术生成




最后,测试程序的编译命令:


along@along2x:~/bill2d/src$ g++ -std=c++14 -o bill2dmain1 bill2d_main.cpp -I/home/along/bill2d/src                                                                                                                    ---I/home/along/bill2d/build -I/usr/include/hdf5/serial -I/home/along/bill2d/build/googletest/src/gtest/googletest/include -L/usr/lib/x86_64-linux-gnu/hdf5/serial -L/home/along/bill2d/build/lib -L/usr/lib -lbill2d -lboost_system -lboost_filesystem -lboost_program_options -lhdf5 -lhdf5_cpp -mavx   


along@along2x:~/bill2d/src$ g++ -o testc testc.c -I/home/along/bill2d/build -I/usr/include/hdf5/serial -I/home/along/bill2d/build/googletest/src/gtest/googletest/include -lbill2d -lboost_system -L/home/along/bill2d/build/lib -mavx


g++ test01p.cpp -I/usr/include/hdf5/serial -I/home/along/bill2d/build -I/home/along/bill2d/build/googletest/src/gtest/googletest/include -L/home/along/bill2d/build/lib -L/usr/lib/x86_64-linux-gnu/hdf5/serial -L/path/to/bill2d/library -L/path/to/boost/library -L/home/along/bill2d/build/lib -L/path/to/hdf5/lib -L/usr/lib/x86_64-linux-gnu -lbill2d -lboost_system -lboost_program_options -lboost_filesystem -lgsl -lhdf5 -lhdf5_cpp -lblas -mavx -o test01p


g++ bill2d_main.cpp -I/usr/include/hdf5/serial -I/home/along/bill2d/build -I/home/along/bill2d/build/googletest/src/gtest/googletest/include -L/home/along/bill2d/build/lib -L/usr/lib/x86_64-linux-gnu/hdf5/serial -L/path/to/bill2d/library -L/path/to/boost/library -L/home/along/bill2d/build/lib -L/path/to/hdf5/lib -L/usr/lib/x86_64-linux-gnu -lbill2d -lboost_system -lboost_program_options -lboost_filesystem -lgsl -lhdf5 -lhdf5_cpp -lblas -mavx -o bill2d_main

/cmake1test/math01F/math02f.h


#define MATHFUNCTIONS_H

class MathFunc{

        public:
                static double square(double x);


};

#include "math02f.h"

double MathFunc::square(double x) {

                return(x*x);
}

/cmake1test/math01F/CMakeLists.txt


add_library( math01F STATIC math02f.cpp)

/cmake1test/main.cpp

#include <iostream>
#include "math01F/math02f.h"

int main() {

        double value=4.0;
        std::cout<<"The square:" <<value<<"is" << MathFunc::square(value)<< std::endl;


        return 0;
}//main()


/cmake1test/CMakeLists.txt


cmake_minimum_required(VERSION 3.10)
project(MyProject VERSION 1.0)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

add_subdirectory(math01F)

add_executable(MyProject main.cpp)

target_link_libraries(MyProject PUBLIC math01F)

mkdir build

cd build

cmake ..

make

运行:

./MyProject

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值