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

2018-12-29  本文已影响0人  sudacq

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!
上一篇 下一篇

猜你喜欢

热点阅读