create_t*create_test = (create_t*) dlsym(test_index, "create");
const char*dlsym_error = dlerror();
if(dlsym_error) {
cerr << "Cannot load symbol create: " << dlsym_error<< '\n';
return 1;
}
destroy_t*destroy_test = (destroy_t*) dlsym(test_index, "destroy");
dlsym_error= dlerror();
if(dlsym_error) {
cerr << "Cannot load symbol destroy: " << dlsym_error<< '\n';
return 1;
}
// create aninstance of the class
test_base*c_test = create_test();
// use theclass
c_test->display();
destroy_test(c_test);
// unloadthe test library
dlclose(test_index);
}
makefile的基本结构不是很复杂,但当一个程序开发人员开始写makefile时,经常会怀疑自己写的是否符合惯例,而且自己写的makefile 经常和自己的开发环境相关联,当系统环境变量或路径发生了变化后,makefile可能还要跟着修改。
在一些大的工程中,我们会把我们不同模块或是不同功能的源文件放在不同的目录中,我们可以在每个目录中都书写一个该目录的makefile,这有利于让我们的makefile变得更加地简洁,而不至于把所有的东西全部写在一个makefile中,这样会很难维护我们的makefile,这个技术对于我们模块编译和分段编译有着非常大的好处。
我们把这个makefile叫做“总控makefile”,总控makefile的变量可以传递到下级的makefile中(如果你显示的声明),但是不会覆盖下层的makefile中所定义的变量,除非指定了“-e”参数。
很多时候,我们在网上下载的linux开源软件都会遇到一个问题,就是源码里面没有直接的makefile,但是它有makefile.am和makefile.in或者它有makefile.am和configure.in,这里就不详细解释他们直接的关联,直接给出如何操作生成makefile。
.SUFFIXES: .c .cpp .o
CC=g++ -g -shared -fPIC
GCC=g++ -g
all:clear test_1.so a.out test_2.so clean
OBJ1=test_1.o
OBJ2=main.o
OBJ3=test_2.o
clear:
rm -rf *.so a.out b.out
test_1.so:$(OBJ1)
$(CC) -o $@ $?
cp $@ /usr/lib
a.out:$(OBJ2)
$(GCC) -o $@ $? -ldl
test_2.so:$(OBJ3)
$(CC) -o $@ $?
cp $@ /usr/lib
.cpp.o:
$(CC) -c $*.cpp
.c.o:
$(CC) -c $*.c
clean:
rm -f *.o
执行makefile正常编译后,可生成test_1.so、test_2.so动态库以及a.out执行程序。可执行a.out,显示结果如下:
[root@localhost c++_so_src]# ./a.out test_1.so
Running in test1.so Now
[root@localhost c++_so_src]# ./a.out test_2.so
Running in test2.so Now
[root@localhost c++_so_src]# ./a.out
Argument Error! You must enter like this:
./a.out test_1.so
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-90978-10.html
放弃那些陈旧的
3亿人
不让回复
生虫也不可信