[英]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.