23,118
社区成员
发帖
与我相关
我的任务
分享
$ cat Makefile
CXXFLAGS = -fPIC
all: libX.so tcaller
libX.so: tlib.o
$(CXX) -shared -o $@ $^ -Wl,-static -static-libgcc
tcaller: tcaller.o
$(CXX) -o $@ $^ -L. -lX -Wl,-static -static-libgcc
clean:
rm -f tlib.o
rm -f tcaller.o
rm -f libX.so
rm -f tcaller
$ make
g++ -fPIC -c -o tlib.o tlib.cpp
g++ -shared -o libX.so tlib.o -Wl,-static -static-libgcc
g++ -fPIC -c -o tcaller.o tcaller.cpp
g++ -o tcaller tcaller.o -L. -lX -Wl,-static -static-libgcc
/usr/bin/ld: warning: type and size of dynamic symbol `__start___libc_atexit' are not defined
/usr/bin/ld: warning: type and size of dynamic symbol `__stop___libc_atexit' are not defined
$ ldd tcaller
libX.so => ./libX.so (0x2ab00000)
$ ldd libX.so
statically linked
$ ./tcaller
int100
Inconsistency detected by ld.so: dl-fini.c: 180: _dl_fini: Assertion `ns != 0 || i == nloaded' failed!
$
$ cat tlib.h
#ifndef TLIB_H
#define TLIB_H
void f(int x);
#endif /* TLIB_H */
$ cat tlib.c
cat: tlib.c: No such file or directory
$ cat tlib.h
#ifndef TLIB_H
#define TLIB_H
void f(int x);
#endif /* TLIB_H */
$ cat tlib.cpp
#include <iostream>
#include "tlib.h"
#include <string>
using namespace std;
void f(int x)
{
cout << "int" << x << endl;
}
$ cat tcaller.cpp
#include "tlib.h"
#include <stdio.h>
int main()
{
f(100);
return 1;
}
$ cat Makefile
CXXFLAGS = -fPIC
all: libX.so tcaller
libX.so: tlib.o
$(CXX) -shared -o $@ $^ -static -lstdc++ -static-libgcc
tcaller: tcaller.o
$(CXX) -o $@ $^ -L. -lX
clean:
rm -f tlib.o
rm -f tcaller.o
rm -f libX.so
rm -f tcaller
$ make clean && make && ./tcaller
rm -f tlib.o
rm -f tcaller.o
rm -f libX.so
rm -f tcaller
g++ -fPIC -c -o tlib.o tlib.cpp
g++ -shared -o libX.so tlib.o -static -lstdc++ -static-libgcc
g++ -fPIC -c -o tcaller.o tcaller.cpp
g++ -o tcaller tcaller.o -L. -lX
int100
$ uname -a
Linux Loongson-1 2.6.18.1lemote #1 Sat Jan 13 16:02:26 CST 2007 mips GNU/Linux
$