Code fails with linker error. Why?
John Colvin via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Oct 4 03:27:16 PDT 2014
On Saturday, 4 October 2014 at 04:02:46 UTC, eles wrote:
> On Friday, 3 October 2014 at 15:47:33 UTC, John Colvin wrote:
>> On Friday, 3 October 2014 at 15:44:16 UTC, eles wrote:
>>> class ShapeSurface(T) {
>>> public:
>>> int formula();
>>
>> that means you have a definition of formula elsewhere (which
>> the linker tries to find, but obviously fails. What you want is
>>
>> class ShapeSurface(T) {
>> public:
>> abstract int formula();
>
> Thanks, but still. The compiler shall not let that code pass
> down to the linker. It has everything it needs to not do that,
> or it shall have. Linker errors shall be simply because the
> libraries are not in place (either not installed, either
> linking path not correctly configured, either broken versions
> of those libraries).
Sorry, but that's just not how it works. There is no requirement
for the definition of a function to be found in the same
compilation unit as it's declaration.
// a.d
void foo();
void main() { foo(); }
// b.d
import std.stdio;
void foo() { writeln("Hello World"); }
$ ls
a.d b.d
$ dmd b.d -c
$ ls
a.d b.d b.o
$ dmd a.d b.o
$ ls
a a.d a.o b.d b.o
$ ./a
Hello World
So the compiler has no way of knowing whether you've forgotten to
include a definition, or whether it's simply sitting in some
other object file / library / whatever. The linker, however, can
know, hence the linker error.
More information about the Digitalmars-d-learn
mailing list