如何在C或C ++源代码我/连接外部的功能呢? [英] How do I source/link external functions in C or C++?

查看:124
本文介绍了如何在C或C ++源代码我/连接外部的功能呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我想我应该澄清,以防它很重要。我是在一个AIX Unix机器,所以我使用VAC编译器 - 没有GNU编译器。
编辑完


I suppose I should clarify, in case it matters. I am on a AIX Unix box, so I am using VAC compilers - no gnu compilers. End edit


我在C / C ++ pretty生锈,所以请原谅我,如果这是一个简单的问题。

I am pretty rusty in C/C++, so forgive me if this is a simple question.

我想借此常用功能出了一些我的C程序,并把它们在共享库或共享对象。如果我在Perl中这样做我会把我的潜艇的Perl模块中,并在需要的时候使用这个模块。

I would like to take common functions out of a few of my C programs and put them in shared libraries or shared objects. If I was doing this in perl I would put my subs in a perl module and use that module when needed.

对于一个例子来说,假设我有这个功能:

For the sake of an example, let's say I have this function:

int giveInteger()
{
    return 1034;
}

显然,这不是一个真实的例子,但如果我想分享的功能,我将如何进行?

Obviously this is not a real world example, but if I wanted to share that function, how would I proceed?

我是pretty知道我有两个选择:

I'm pretty sure I have 2 options:


  1. 放在一个文件我的共享功能,并将它与我在编译时主程序编译。如果我修改我的共享功能,我将不得不重新编译我的主要程序。

  2. 放在一个文件我的共享功能,并将其编译为一个共享库(如果我有我来说是正确的),并有我的主要程序链接到共享库。我对我的共享库(编译后)的任何更改将在运行时融入了我的主程序,而无需重新编译我的主要程序。

我是正确的这种想法?

如果是这样,我怎么能complish非此即彼/这两方面的方法呢?我搜索了很多,我似乎找到了如何如何,我可以有我自己的程序链接到别人的共享库的信息,而不是如何创建自己的共享功能,在某种程度上我可以在自己的程序中使用它们编译它们

If so, how can I complish either/both of those methods? I've searched a lot and I seem to find information how how I could have my own program link to someone else's shared library, but not how to create my own shared functions and compile them in a way I can use them in my own program.

太感谢了!

布赖恩


编辑:

结论


谢谢大家对你的帮助!我以为我会加入到这个职位是什么工作对我来说(动态共享库在AIX上),这样其他人可以受益:

Conclusion

Thanks everyone for your help! I thought I would add to this post what is working for me (for dynamic shared libraries on AIX) so that others can benefit:

我编译我的共享功能:

xlc -c sharedFunctions.c -o sharedFunctions.o

然后使其成为一个共享对象:

Then make it a shared object:

xlc -qmkshrobj -qexpfile=exportlist sharedFunctions.o
xlc -G -o libsharedFunctions.so sharedFunctions.o  -bE:exportlist

然后连接另一个程序:

Then link it another program:

xlc -brtl -o mainProgram mainProgram.c  -L. -lsharedFunctions

和其他评论帮我找到这个环节,这也帮助:
<一href=\"http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/topic/com.ibm.vacpp7a.doc/proguide/ref/compile_library.htm\" rel=\"nofollow\">http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/topic/com.ibm.vacpp7a.doc/proguide/ref/compile_library.htm

And another comment helped me find this link, which also helped: http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/topic/com.ibm.vacpp7a.doc/proguide/ref/compile_library.htm

再次感谢所有谁帮我出去!

Thanks again to all who helped me out!

推荐答案

是的,你是正确的。第一个被称为的静态库的,而第二个被称为的共享库的,因为code未绑定在编译时可执行文件,但每次又当你的程序被加载。

Yeah you are correct. The first is called a static library, while the second is called a shared library, because the code is not bound to the executable at compile time, but everytime again when your program is loaded.

编译图书馆的code如下:

Compile your library's code as follows:

gcc -c *.c

-c 告诉程序没有到目标文件链接,只是让你为每个目标文件 .C 文件编译的。现在,它们归档到一个静态库:

The -c tells the program not to link the object file, but just leaves you with object files for each .c file that was compiled. Now, archive them into one static library:

ar rcs libmystuff.a *.o

男人AR 会告诉你什么RCS的选项的含义。现在,libmystuff.a是一个 rchive可文件包含这些目标文件为每个目标文件的符号索引(你可以用一些压缩文件查看器打开它),共同提高。你可以将其链接到你的程序:

man ar will tell you what the rcs options mean. Now, libmystuff.a is a archive file (you can open it with some zip-file viewers) which contain those object files, together with an index of symbols for each object file. You can link it to your program:

gcc *.c libmystuff.a -o myprogram

现在,你的程序已准备就绪。注意,其中的静态库出现在命令事项的顺序。看到我的链接订单回答。

Now, your program is ready. Note that the order of where the static libraries appear in the command matter. See my Link order answer.

有关共享库,你将创建你的图书馆

For a shared library, you will create your library with

gcc -shared -o libmystuff.so *.c

这一切都需要,libmystuff.so现在是一个取值 hared 0 bject文件。如果你想要一个程序链接到它,你必须把它变成在 /etc/ld.so.conf中文件中列出的目录,或者是给予由 -L 切换到GCC,或者在LD_LIBRARY_PATH变量中列出。链接时,你砍了 LIB preFIX和的.so 从你告诉GCC库名称后缀。

That's all it takes, libmystuff.so is now a shared object file. If you want to link a program to it, you have to put it into a directory that is listed in the /etc/ld.so.conf file, or that is given by the -L switch to GCC, or listed in the LD_LIBRARY_PATH variable. When linking, you cut the lib prefix and .so suffix from the library name you tell gcc.

gcc -L. -lmystuff *.c -o myprogram

在内部,gcc会只是通过你的论点到GNU链接。你可以看到它通过使用什么样的​​参数 - ### 选项:GCC将打印给每个子流程的具体参数。

Internally, gcc will just pass your arguments to the GNU linker. You can see what arguments it pass using the -### option: Gcc will print the exact arguments given to each sub process.

有关链接过程(怎么有些东西是内部完成的),查看我的的Linux GCC连接器的详细信息的答案。

For details about the linking process (how some stuff is done internally), view my Linux GCC linker answer.

这篇关于如何在C或C ++源代码我/连接外部的功能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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