简体   繁体   English

当我链接到库时,为什么会出现错误,而不是在构建它时出错?

[英]Why do I get errors when I link to a library, but not when I build it?

I'm working on an external module for a c++ library (ITK), which I'm building with cmake and make (I'm on an Ubuntu VM). 我正在为c ++库(ITK)开发一个外部模块,我正在用cmake和make构建(我在Ubuntu VM上)。

Let's say there is an error in the code, such as redefinition of a method. 假设代码中存在错误,例如重新定义方法。 In this case, the library builds without errors with cmake ../src && make && sudo make install , but I get an error when I try to link a project against the library. 在这种情况下,库使用cmake ../src && make && sudo make install构建时没有错误cmake ../src && make && sudo make install ,但是当我尝试将项目链接到库时出现错误。

Why is it that the errors don't stop the library build? 为什么错误不会阻止库构建? Is there something fundamentally different about these scenarios that causes make to behave differently? 这些场景是否存在根本不同的因素导致make表现不同?

I'm fairly new to c++ (and to compiled languages generally)--I imagine there is something fundamental about the build process that I'm not understanding. 我对c ++(以及一般的编译语言)相当陌生 - 我认为构建过程有一些基本的东西,我不理解。 I found it difficult to search for this--searching for "c++ library build no error" gives me a lot of threads on c++ library build errors. 我发现很难找到这个 - 搜索“c ++ library build no error”给了我很多关于c ++库构建错误的线程。

In general, libraries are simply collections of compiled code - object files. 通常,库只是已编译代码的集合 - 对象文件。 As a rule the linkage errors that you're seeing will only be picked up when you create an executable at which point the linker will try to ensure that all symbols used in your executable can be resolved with the libraries that you're linking against. 通常,只有在创建可执行文件时才会获取您所看到的链接错误,此时链接器将尝试确保可执行文件中使用的所有符号都可以使用您要链接的库来解析。

If the definition of a symbol (function, constant etc) either cannot be found or is ambiguous (ie can be found in more than one place, then linking will fail. 如果无法找到符号(函数,常量等)的定义或模糊不清(即可以在多个地方找到,则链接将失败。

While linking, it checks for cross-references, symbols (functions, identifiers etc) definition and call or use gets binded in static linking (dynamic linking is different where cross-referencing may not be done as this is deferred linking). 在链接时,它检查交叉引用,符号(函数,标识符等)定义和调用或使用在静态链接中绑定(动态链接是不同的,其中交叉引用可能不会,因为这是延迟链接)。 Library is a simple collection of function or identification definition, doesn't do cross-referencing. 库是函数或标识定义的简单集合,不做交叉引用。

暂无
暂无

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

相关问题 当符号明确存在于我链接的静态库中时,为什么会出现链接错误? - Why do I get link errors when the symbol is clearly present in the static library I link against? 为什么会出现这些链接错误? - Why do I get these link errors? 当我要在C ++ Builder Starter中链接静态库TinyXML 2的发行版时,为什么会出现无法解决的外部错误? - Why I get unresolved external errors when I want to link the release version of the static library TinyXML 2 in C++ Builder Starter? 在C ++中调用库(构建库成功)时,为什么会出现链接错误 - Why will I have link errors when invoking a library (building the library is successful) in C++ 为什么在尝试创建 std::numpunct 实例时出现链接错误<char16_t>衍生物? - Why do I get link errors when trying to create an instance of std::numpunct<char16_t> derivative? 为什么我初始化数组容器的元素大于数组大小时没有出错? - Why I do not get errors when I initialize an array container with more elements than the size of the array? 尝试在C ++中包含头文件时为什么会出现错误? - Why do I get errors when I try to include a header file in C++? 当我尝试使这个构造函数通用时,为什么会出现编译错误? - Why do I get compile errors when I try to make this constructor generic? SDL构建系统 - 当我链接到`gnustl_shared`时,我得到“错误:无法在运行时加载库:...” - SDL build system - When I link against `gnustl_shared` I get “Error: Cannot load library: …” at runtime 在库中使用 fstream 时,我在可执行文件中收到链接器错误 - When using fstream in a library I get linker errors in the executable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM