sudo make install
sudo make install

Reputation: 5669

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).

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.

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?

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. 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.

Upvotes: 3

Views: 446

Answers (2)

Dr. Debasish Jana
Dr. Debasish Jana

Reputation: 7128

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.

Upvotes: 1

Component 10
Component 10

Reputation: 10507

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 (i.e. can be found in more than one place, then linking will fail.

Upvotes: 1

Related Questions