linux用c++调用动态库
生活随笔
收集整理的這篇文章主要介紹了
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
用 沒有 .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++调用动态库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 持续高温引发百姓热议 ***趁机放毒谋取
- 下一篇: InfoPath发布要点