CMakeLists.txt编写基础知识
程序员文章站
2022-07-15 16:44:59
...
参考文献
应用实例
说明
采用c++的类来写hello world,然后编写教程。
附录
#include "hello.hh"
int main()
{
HelloFunc hi;
hi.Hello();
return 0;
}
- hello.hh
#ifndef HELLO_hh
#define HELLO_hh 1
class HelloFunc
{
public:
HelloFunc();
virtual ~HelloFunc();
void Hello();
private:
};
#endif
#include "hello.hh"
#include "iostream"
using namespace std;
HelloFunc::HelloFunc()
{
}
HelloFunc::~HelloFunc()
{}
void HelloFunc::Hello()
{
cout<<"Hello World"<<endl;
}
-CMakeList.txt 编译文件
##
## cmake module authored by Hubery Lee(李会)
## 2018年11月22日
##
cmake_minimum_required(VERSION 2.6 FATAL_ERROR) #
set(PROJ_NAME hello)
project(${PROJ_NAME})
#project(${PROJ_NAME} C CXX Fortran)
#enable_language(Fortran) ##允许使用语言类型
#set(CMAKE_BUILD_TYPE release) ##release or debug
#set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}") ##
#------------------------------------
# Include directory for this project
#
include_directories(${PROJECT_SOURCE_DIR}/inc)
#------------------------------------
# Locate sources and headers for this project
#
file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc)
file(GLOB headers ${PROJECT_SOURCE_DIR}/inc/*.hh)
#------------------------------------
# Add the executable and link it to the libraries
#
add_executable(${PROJ_NAME} main.cc ${sources} ${headers})
#target_link_libraries(${PROJ_NAME} -L${PROJECT_SOURE_DIR}/lib hello.so) ##链接库文件
#-------------------------------------
# Install the executable or files to 'bin' directory
# under CMAKE_INSTALL_PREFIX
#
install(TARGETS ${PROJ_NAME} DESTINATION bin)
- 问题描述
- 如何外部编译 ?
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd)/lib
cd build
cmake -DCMAKE_INSTALL_PREFIX=../ ../
make -j4
make install
cd ../bin
- 设置二进制文件存放地址
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
- 如何生成动态库?
待续