美文网首页
cling 添加用户编译的动态库支持

cling 添加用户编译的动态库支持

作者: sudacq | 来源:发表于2018-12-29 12:37 被阅读0次

1. 基本用法

#pragma cling add_include_path("inc_directory")
#pragma cling add_library_path("lib_directory")
#pragma cling load("libname")

2. 编译生成动态库

2.1. 书写代码

hello.h

#include <iostream>
using namespace std;
void print();

hello.cpp

#include "hello.h"
void print()
{
   cout << "hello world!\n";
}

2.2. 使用g++生成动态链接文件(.so文件)

$ g++ -fPIC --shared hello.cpp -o libhello.so

2.3. 验证生成的.so文件可用

main.cpp

#include "hello.h"
int main()
{
        print();
        return 0;
}

链接并运行

$ g++ main.cpp -L . -lhello -o main && ./main
hello world!

3. 使用cling测试

[cling]$ #pragma cling add_include_path("/home/user/libtmp/")
[cling]$ #pragma cling add_library_path("/home/user/libtmp")
[cling]$ #pragma cling load("libhello.so")
[cling]$ #include "hello.h"
[cling]$ print()
hello world!

相关文章

网友评论

      本文标题:cling 添加用户编译的动态库支持

      本文链接:https://www.haomeiwen.com/subject/cvpulqtx.html