简体   繁体   English

gcc编译和链接

[英]gcc compiling and linking

I created a shared library in this way 我以这种方式创建了一个共享库

gcc -I/home/lib 'pkg-config --cflags gtk+-2.0 libxml-2.0' -shared -fPIC -Wl,--export-dynamic file1.c file2.c -o lib.so

it works and the library created also works for what I need. 它可以工作,创建的库也可以满足我的需要。 What I want to know is where the compiling and the linking are in this command string, so please explain it to me and tell me a way to divide them in two different commands, just to understand better. 我想知道的是此命令字符串中的编译和链接在哪里,因此请向我解释一下,并告诉我将它们分为两个不同命令的方法,以更好地理解。 I need to understand this because I can't not explain why this library works, even if this has to be linked to another one that I've never linked to it. 我需要理解这一点,因为我无法解释该库为何起作用,即使该库必须链接到我从未链接过的另一个库也是如此。

The gcc -c option is for compile only. gcc -c选项仅用于编译。 However, I'm not sure if that is what you want. 但是,我不确定这是否是您想要的。

Your command compiles and links in one invocation. 您的命令在一次调用中进行编译和链接。

You can split the command into compiling and linking commands: 您可以将命令分为编译和链接命令:

gcc -c -o file1.o -Wall -Wextra -Werror -I/home/lib $(pkg-config --cflags gtk+-2.0 libxml-2.0) -fPIC file1.c
gcc -c -o file2.o -Wall -Wextra -Werror -I/home/lib $(pkg-config --cflags gtk+-2.0 libxml-2.0) -fPIC file2.c
gcc -shared -o lib.so file1.o file2.o

GCC uses the extensions in the file names to determine what type they are and what to do with them. GCC使用文件名中的扩展名来确定它们是什么类型以及如何使用它们。 So the fact that you listed some names ending with “.c” tells GCC to compile them as C code. 因此,您列出了一些以“ .c”结尾的名称的事实告诉GCC将其编译为C代码。 If you had given only names ending with “.o”, GCC would treat them as object files and would not compile them. 如果仅给出以“ .o”结尾的名称,则GCC会将其视为目标文件,而不会编译它们。

After compiling, GCC by default links to make an executable. 编译后,默认情况下,GCC链接以生成可执行文件。 The “-shared” switch tells it to make a shared library instead. “共享”开关告诉它创建一个共享库。

To compile only, without linking, you could use the switch “-c” and remove the “-shared” switch, and also remove any of the command arguments or switches that are only used for linking (“-Wl,--export-dynamic” is one, and I do not know whether that pkg-config will produce any link options). 要仅进行编译而不进行链接,可以使用开关“ -c”并删除“共享”开关,还可以删除任何仅用于链接的命令参数或开关(“ -Wl,-export-动态”是其中之一,我不知道pkg-config是否会产生任何链接选项)。 Also remove or change the “-o lib.so”, since that sets the name to use for the output file, and it is not what you want for object files that result from compiling only. 还要删除或更改“ -o lib.so”,因为这将设置要用于输出文件的名称,而这并不是仅通过编译生成的目标文件所需要的名称。

To link only, without compiling, you would simply list object files instead of source files and remove the switches affecting compilation (“-I/home/lib”, “-fPIC”, and any produced by pkg-config). 要仅链接而不进行编译,您只需列出目标文件而不是源文件,并删除影响编译的开关(“ -I / home / lib”,“-fPIC”以及pkg-config产生的任何开关)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GCC:编译应用程序而不链接任何库 - GCC: compiling an application without linking any library GCC:我如何使这个编译和链接工作? - GCC: How can I make this compiling and linking work? 用gcc编译基本x86代码时出现链接错误 - Linking error when compiling basic x86 code with gcc 手动运行gcc的步骤,编译,组装,链接 - Running gcc's steps manually, compiling, assembling, linking GCC在编译时在/ usr / local / include中查找头文件,但在链接时不查找/ usr / local / lib中的库。为什么? - GCC looks for headers in /usr/local/include when compiling, but not for libraries in /usr/local/lib when linking. Why? 使用gcc编译文件时链接fftw-3.3.6-pl2 - Linking fftw-3.3.6-pl2 while compiling my file using gcc 编译+链接自定义操作系统与Cygwin gcc:无法识别的仿真模式:elf_i386 - Compiling + linking a custom OS with Cygwin gcc: Unrecognized emulation mode: elf_i386 与将.c文件制成.o文件并链接和组装相比,编译gcc .c文件有什么区别? - What is the difference compiling gcc .c file in comparison to making a .c file into an .o file and linking and assembling? 使用 arm-none-eabi-gcc 编译并链接库 liba.a 错误 - Compiling using arm-none-eabi-gcc and linking library liba.a error gcc:链接外部库 - gcc: Linking an External Library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM