Dmd linker (OPTLINK) gives error 42: Undefined character when using extern

Linking the following two files gives me a link error:

ad:

import std.stdio; extern string test (); void main() { writeln(test()); readln(); } 

bd:

 string test () { return "hello"; } 

I get an error:

 Error 42: Symbol Undefined _D1a4testFZAya` ---errorlevel 1 

What's wrong?

_________________________________________________

Edit: this is the correct way to do this:

ad:

 import std.stdio; import b; void main() { writeln("some_var from Module b: \"", b.some_var, "\""); } 

bd:

 public string some_var = "Hello, world!"; //you can also use static module constructors to set your vars static this() { some_var ~= " -- How are you?"; } 

This code was kindly provided by Joshua Reusch to the excellent D beginners forum on digitalmars.com.

+4
source share
1 answer

Change your ad to:

 import std.stdio; import b; //extern string test (); void main() { writeln(test()); readln(); } 

extern is a binding attribute and is mainly used to indicate which calling convention to use for a given function (usually the C function in some library). Read more about extern and other attributes here: http://www.d-programming-language.org/attribute.html . If you have D source files, there is no need for extern. However, if you mix C or C ++ and D code, you will definitely have to use it.

+4
source

Source: https://habr.com/ru/post/1387959/


All Articles