日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux用c++调用动态库

發布時間:2025/5/22 linux 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux用c++调用动态库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.3 用c++靜態方式調用動態庫libsthc.so: /*cpptest.cc*/??? //linux下的c++后綴有cc,cxx, cpp #include "libsthc.h" using namespace std;???????????? //std命名空間 用 .h 的頭文件,就不用 using namespace std
用 沒有 .h 的頭文件,就 必須 用 using namespace std
例如:
#include <iostream.h>
#include <string.h>
不用
-----------------------------
例如:
#include <iostream>
#include <string>
using namespace std;
必須 用
-----------------------------

using namespace std; 是 "用命名空間中的定義"。
std 是 空間 名,“標準”的意思。
現在一般推薦用 無 .h 的 頭文件,寫using ...
int main(void) { printf("%d\n", add(1, 2)); return 0; } #makefile: cpptest:cpptest.o g++ cpptest.o –o cpptest -lsthc? //-lsthc庫文件,缺省路徑在/usr/lib,對于其他路徑例如:-L /usr/local/lib -lsthc cpptest.o:cpptest.cc g++ -c cpptest.cc -Wno-deprecated -o cpptest.o? //-Wno-deprecated 對廢棄的特性不予警告 all:cpptest clean: rm -f *.o cpptest 1.4 用c++動態方式調用動態庫libsthc.so: /*cppdltest.cpp*/ #include "stdio.h" #include "stdlib.h" #include "dlfcn.h"?? //也是用的這個文件,和c一樣 int main(void) { void *handle; int (*fcn)(int x, int y); const char *errmsg; /* open the library */ handle = dlopen("libsthc.so", RTLD_NOW); if(handle == NULL) { fprintf(stderr, "Failed to load libsthc.so: %s\n", dlerror()); return 1; } dlerror(); *(void **)(&fcn) = dlsym(handle, "add");???? //ok //fcn = dlsym(handle, "add");??????????????????????? //not ok in c++ if((errmsg = dlerror()) != NULL) { printf("%s\n", errmsg); return 1; } printf("%d\n", fcn(1, 5)); dlclose(handle); return 0; } #makefile cppdltest:cppdltest.o g++ cppdltest.o -ldl -lsthc -o cppdltest cppdltest.o:cppdltest.cpp g++ -c cppdltest.cpp -o cppdltest.o all:cppdltest clean: rm -f *.o cppdltest

轉載于:https://blog.51cto.com/no001/346062

總結

以上是生活随笔為你收集整理的linux用c++调用动态库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。