动态链接
目录:
文件:
Hello.h
#ifndef CMAKER_LEARNING_HELLO_H
#define CMAKER_LEARNING_HELLO_H
class Hello{
public:
static void print();
};
#endif //CMAKER_LEARNING_HELLO_H
Hello.cpp
#include <iostream>
#include "../include/shared/Hello.h"
using namespace std;
void Hello::print() {
cout << "hello Shared Library!" << endl;
}
main.cpp
#include "../include/shared/Hello.h"
int main(int argc, char *argv[]) {
Hello hi;
hi.print();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(hello_library)
############################################################
# 1) create a library
############################################################
# generate the shared library from the library sources
add_library(hello_library SHARED src/Hello.cpp)
# 别名
add_library(hello::library ALIAS hello_library)
target_link_libraries(hello_library
PUBLIC
${PROJECT_SOURCE_DIR}/include
)
############################################################
# 2) create an executable
############################################################
add_executable(hello_binary src/main.cpp)
target_link_libraries(hello_binary PRIVATE hello::library)
运行:
cd build
cmake ..
make
./hello_binary