简体   繁体   English

如果我在GCC中编译和链接不需要的库会发生什么?

[英]What happens if I compile and link with unneeded libraries in GCC?

I guess the title is slightly confusing but I will try and clarify what I mean by that. 我想这个标题有点令人困惑,但我会尝试澄清我的意思。 Below I have attached my make file. 下面我附上了我的make文件。 As you can see on the fourth line I am selecting all the libraries I want my project to link against. 正如你在第四行看到的那样,我选择了我希望项目链接的所有库。

When the project compiles I see that for every object file it is creating it is linking with all the libraries specified with the LIBRA line. 当项目编译时,我看到它正在创建的每个目标文件都链接到使用LIBRA行指定的所有库。 Not all of those libraries are required by all the files in my project. 并非所有这些库都是我项目中的所有文件都需要的。 Perhaps just one or two use across each .cpp file. 也许在每个.cpp文件中只使用一两个。

Does this produce any additional cost to the compilation process? 这会对编译过程产生任何额外成本吗? Does this create a larger binary? 这会创建更大的二进制文件吗? Are modern compilers good enough that this is not a problem or do I have to read some more literature on compilers? 现代编译器是否足够好,这不是问题,还是我必须阅读更多关于编译器的文献?

CC=g++
CFLAGS=-c -Wall -std=c++11 -g 
LDFLAGS+=-ldl
LIBRA= -lboost_system -lboost_filesystem -lboost_thread -lsigar-x86_64-linux -ldl -lsqlite3
LIBB=-L/home/tensai/SIGAR/lib -L/usr/include -L/usr/local/lib
SOURCES=main.cpp config_reader.cpp database_manager.cpp monitor_agent.cpp analyze_agent.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=lnmp
INCLUDES = -I/home/tensai/SIGAR

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(LDFLAGS) $(OBJECTS) $(INCLUDES) $(LIBB) -o $@ $(LIBRA)

.cpp.o:
    $(CC) $(CFLAGS) $(INCLUDES) $(LIBB) $< -o $@ $(LIBRA)

clean: 
    rm *.o

Static libraries are only pulled in if they offer symbols that are undefined at this point in the linking process. 只有在链接过程中提供未定义的符号时,才会引入静态库。 If they do not contain such symbols they are ignored. 如果它们不包含这些符号,则会被忽略。 In some rare cases unnecessary libraries can still hurt, because they define symbols that are supposed to be resolved by system libraries that come later on the command line, in this case the linker takes the code from whatever library comes first. 在极少数情况下,不必要的库仍然可能会受到影响,因为它们定义了应该由稍后在命令行上的系统库解析的符号,在这种情况下,链接器从任何库中获取代码。 However, this is really rare. 但是,这真的很少见。
With dynamic libraries it's different: If you feed unneeded dynamic libraries (.so) to the linker it may well put a reference to them into the executable, and you better have those libraries available at runtime. 对于动态库,它是不同的:如果您将不需要的动态库(.so)提供给链接器,它可能会将它们引用到可执行文件中,您最好在运行时使用这些库。 The binary don't get bigger here, but it hurts if you plan to distribute some executables separately: 二进制文件在这里不会变大,但是如果你打算单独分发一些可执行文件会很痛苦:

$ cat x.c
int main(){return 14;}

$ gcc x.c -o xx -l ssl3 -L .

$ ./xx
./xx: error while loading shared libraries: libssl3.so: cannot open shared object file: No such file or directory

$ gcc x.c -o xx

$ ./xx

$

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

相关问题 如何获取我的makefile来编译/链接旧的和较新的GCC / GNU库? C ++ - How can I get my makefile to compile/link older and newer GCC/GNU libraries? C++ 如果我使用更新版本的gcc编译二进制文件,如何在客户端linux机器上找到需要安装的库? - How do I find what libraries need to be installed on a client linux machine if I compile a binary with a newer version of gcc? 当我的代码使用ICC时,链接到GCC构建的库 - Link to libraries built by GCC when I use ICC for my code 如何编译wth gcc,intel静态库? - How to compile wth gcc , intel static libraries? gcc(g ++)如何链接许多库? - gcc (g++) How to link many libraries? gcc在编译时或链接时的可见性 - gcc's fvisibility at compile time or link time 带有 GCC 的调试标志(编译阶段/链接阶段) - Debug Flag With GCC (compile phase / link phase) 我可以用 CMake 包含和链接库的最简单方法是什么? - What's the easiest way I can include & link libraries with CMake? 我怎么知道Ubuntu 11.04中gcc,g ++ / c ++的“默认包含目录”,“默认链接目录”和“默认链接库”? - How do I know the “default include directories”, “default link directories” and “default link libraries” of gcc, g++/c++ in Ubuntu 11.04? 我需要链接哪些库来构建googlemock示例? - What libraries do I need to link to build a googlemock example?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM