@我不是匠人
2024-06-21
#ifndef MYAPP_FUNCTIONS_H
#define MYAPP_FUNCTIONS_H
int add(int x, int y);
#endif
#include "function.h"
int add(int x, int y) {
return x + y;
}
#include <iostream>
#include "function.h"
int main()
{
std::cout << add(1, 2) << std::endl;
return 0;
}
g++ main.cpp function.c
# 生成动态库
gcc -shared -fPIC -o libfunc.so function.c function.h
# 链接动态库
g++ main.cpp -L. libfunc.so
#ifdef __cplusplus
extern "C" {
#endif
int add(int x, int y);
#ifdef __cplusplus
}
#endif
通过extern "C"{}来告诉编译器,这里面是C语言的代码。
extern "C"{
#include "function.h"
}
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
希望大家多多点赞,多多转发。