创建.so文件 [英] Creation of.so of files

查看:171
本文介绍了创建.so文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中有-个扩展名为.с的文件:avl_tree.c,buf_read.c,db_prep.c,file_process.c,global_header.c,traverser.c。除jni.h外,使用的包含文件位于文件夹/usr/gcc/4.4/bin/include中,而库位于文件夹/usr/gcc/4.4/bin/lib中。如何从他们创建文件(如果可能的话,请在此命令中指定所有选项)?我对通过JNI创建方法的本机感兴趣。

解决方案

您确实应该阅读GCC的文档。尤其是调用GCC 程序库howto 也是相关的。



通常,会使用某些生成器来驱动生成。经常使用 GNU make 并具有良好的教程文档。如果您的 Makefile -s很复杂,则可能还需要使用 GNU remake 对其进行调试( remake make 的调试变量)。



您通常希望将每个C源文件编译成位置独立代码,因为共享对象具有PIC代码。您可以使用

  gcc -Wall -fPIC -o foo.pic.o foo.c 

将C源代码 foo.c 编译为位置无关的目标文件 foo.pic.o ,您可能需要其他一些编译器选项(例如 -I 添加包含目录,或 -D 定义一些预处理器符号, -g 调试,以及 -O

我强烈建议使用 -Wall 启用几乎所有警告(并且改进代码,直到没有任何警告;这将提高代码的质量)。



然后,您必须链接所有这些 *。pic.o 文件一起与共享对象一起

  gcc -shared * .pic.o -o foo.so 

您可以将某些共享库链接到共享对象中。 / p>

您可能需要阅读 Levine的有关链接程序和装载程序的书



当然,如果您使用GNU make Makefile 中的所有规则。



您可以使用 GNU libtool 也是如此。



也许 dlopen(3)可能会让您感兴趣。


There is a set with - files with extension.с: avl_tree.c, buf_read.c, db_prep.c, file_process.c, global_header.c, traverser.c. Used include files are in folder/usr/gcc/4.4/bin/include except for jni.h, and libraries are in folder/usr/gcc/4.4/bin/lib. How from them to create.so the file (if it is possible specify all options in this command)? It me interests in communication by creation of native of methods by means of JNI.

解决方案

You really should read the documentation of GCC. Notably invoking GCC. The program library howto is also relevant.

Very often, some builder is used to drive the build. GNU make is often used and has a good tutorial documentation. If your Makefile-s are complex, you may also want to use GNU remake to debug them (remake is a debugging variant for make).

You usually want to compile each individual C source file into position independent code because shared objects have PIC code. You can use

 gcc -Wall -fPIC -o foo.pic.o foo.c

to compile a C source foo.c into a position independent object file foo.pic.o and you may need some other compiler options (e.g. -I to add include directories, or -D to define some preprocessor symbols, -g for debugging, and -O for optimizing).

I strongly suggest to enable almost all warnings with -Wall (and to improve your code till no warnings are given; this will improve a little bit your code's quality).

Then you have to link all these *.pic.o files together into a shared object with

 gcc -shared *.pic.o -o foo.so

You can link some shared libraries into a shared object.

You may want to read Levine's book on linkers and loaders

Of course if you use GNU make you'll have rules in your Makefile for all this.

You could use GNU libtool also.

Maybe dlopen(3) could interest you.

这篇关于创建.so文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆