64,654
社区成员
发帖
与我相关
我的任务
分享
[User:root Time:16:46:48 Path:/home/liangdong/cpp]$ ll
total 12
-rw-r--r--. 1 root root 120 May 4 16:46 libtest.cpp
-rw-r--r--. 1 root root 85 May 4 16:45 libtest.h
-rw-r--r--. 1 liangdong daemon 141 May 4 16:42 main.cpp
[User:root Time:16:46:48 Path:/home/liangdong/cpp]$ cat libtest.cpp libtest.h main.cpp
#include "libtest.h"
#include <iostream>
using namespace std;
void test::function() {
cout<<"test::function"<<endl;
}
#ifndef _H_TEST
#define _H_TEST
class test {
public:
void function();
};
#endif
#include <iostream>
#include "libtest.h"
using namespace std;
int main(int argc, char* const argv[]) {
test t;
t.function();
return 0;
}
[User:root Time:16:46:55 Path:/home/liangdong/cpp]$ g++ -o libtest.so -fPIC -shared libtest.cpp -I.
[User:root Time:16:47:29 Path:/home/liangdong/cpp]$ g++ -o main main.cpp -L. -ltest
[User:root Time:16:47:48 Path:/home/liangdong/cpp]$ ll
total 28
-rw-r--r--. 1 root root 120 May 4 16:46 libtest.cpp
-rw-r--r--. 1 root root 85 May 4 16:45 libtest.h
-rwxr-xr-x. 1 root root 5773 May 4 16:47 libtest.so
-rwxr-xr-x. 1 root root 5876 May 4 16:47 main
-rw-r--r--. 1 liangdong daemon 141 May 4 16:42 main.cpp
[User:root Time:16:47:50 Path:/home/liangdong/cpp]$ ./main
./main: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
[User:root Time:16:47:51 Path:/home/liangdong/cpp]$ export LD_LIBRARY_PATH=.
[User:root Time:16:48:01 Path:/home/liangdong/cpp]$ ./main
test::function
[User:root Time:16:48:04 Path:/home/liangdong/cpp]$