当前位置: 技术问答>linux和unix
g++编译动态联接库及调用(源码)
来源: 互联网 发布时间:2015-10-01
本文导语: 我想在QT下调用自己的.so //test.h #ifndef __TEST_H #define __TEST_H //这是在C++程序中声明标准C方式联接的方法 #ifdef __cplusplus extern "C" { #endif #ifdef SHARED int ( *getValue)( int a); #else int getValue( int a); #endif #ifdef __cpl...
我想在QT下调用自己的.so
//test.h
#ifndef __TEST_H
#define __TEST_H
//这是在C++程序中声明标准C方式联接的方法
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef SHARED
int ( *getValue)( int a);
#else
int getValue( int a);
#endif
#ifdef __cplusplus
}
#endif
//test.cpp
/* test.cpp */
#include "test.h"
int getValue( int a)
{
return a;
}
command:
g++ -g -c -fPIC -o test.o test.cpp
ok!
g++ -g -shared -Wl -o my.so test.o -lc
OK!
/****main.cpp***/
#include
#include /* 包含动态链接功能接口文件 */
#define SOFILE "./my.so" /* 指定动态链接库名称 */
#define SHARED
#include"test.h"
main()
{
void *dp;
char *error;
// int (*getValue a);
dp=dlopen(SOFILE,RTLD_LAZY); /* 打开动态链接库 */
if (dp==0) /* 若打开失败则退出 */
{
fputs(dlerror(),stderr);
}
getValue=dlsym(dp,"getValue");
error=dlerror(); /* 检测错误 */
if (error) /* 若出错则退出 */
{
fputs(error,stderr);
}
int b = getValue( 100 ); /* 调用此共享函数 */
printf( "the result is: %dn", b);
dlclose(dp); /* 关闭共享库 */
return 1; /* 成功返回 */
}
[root@localhost rain_dll]# g++ -g -o dy dy.cpp -ldl
In file included from /usr/include/c++/3.2.2/backward/iostream.h:31,
from dy.cpp:1:
/usr/include/c++/3.2.2/backward/backward_warning.h:32:2: warning: #warning This
file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples
include substituting the header for the header for C++ includes, or instead of the deprecated header . To disable this warning
use -Wno-deprecated.
dy.cpp: In function `int main()':
dy.cpp:20: invalid conversion from `void*' to `int (*)(int)'
???????????????????invalid conversion from `void*' to `int (*)(int)'????????????????
//test.h
#ifndef __TEST_H
#define __TEST_H
//这是在C++程序中声明标准C方式联接的方法
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef SHARED
int ( *getValue)( int a);
#else
int getValue( int a);
#endif
#ifdef __cplusplus
}
#endif
//test.cpp
/* test.cpp */
#include "test.h"
int getValue( int a)
{
return a;
}
command:
g++ -g -c -fPIC -o test.o test.cpp
ok!
g++ -g -shared -Wl -o my.so test.o -lc
OK!
/****main.cpp***/
#include
#include /* 包含动态链接功能接口文件 */
#define SOFILE "./my.so" /* 指定动态链接库名称 */
#define SHARED
#include"test.h"
main()
{
void *dp;
char *error;
// int (*getValue a);
dp=dlopen(SOFILE,RTLD_LAZY); /* 打开动态链接库 */
if (dp==0) /* 若打开失败则退出 */
{
fputs(dlerror(),stderr);
}
getValue=dlsym(dp,"getValue");
error=dlerror(); /* 检测错误 */
if (error) /* 若出错则退出 */
{
fputs(error,stderr);
}
int b = getValue( 100 ); /* 调用此共享函数 */
printf( "the result is: %dn", b);
dlclose(dp); /* 关闭共享库 */
return 1; /* 成功返回 */
}
[root@localhost rain_dll]# g++ -g -o dy dy.cpp -ldl
In file included from /usr/include/c++/3.2.2/backward/iostream.h:31,
from dy.cpp:1:
/usr/include/c++/3.2.2/backward/backward_warning.h:32:2: warning: #warning This
file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples
include substituting the header for the header for C++ includes, or instead of the deprecated header . To disable this warning
use -Wno-deprecated.
dy.cpp: In function `int main()':
dy.cpp:20: invalid conversion from `void*' to `int (*)(int)'
???????????????????invalid conversion from `void*' to `int (*)(int)'????????????????
|
包涵头文件不是c++标准 多个warning
main前面加上 int
main前面加上 int