On Tuesday, 2 August 2016 at 17:25:21 UTC, Steven Schveighoffer wrote:
On 8/2/16 1:04 PM, Rufus Smith wrote:
On Tuesday, 2 August 2016 at 16:30:08 UTC, Adam D. Ruppe wrote:
On Tuesday, 2 August 2016 at 16:21:07 UTC, Rufus Smith wrote:
How does one use C main? extern C?

extern(C) int main()

should do it

It doesn't seem to be that easy!

https://wiki.dlang.org/Runtime_internals

This is just explaining how the runtime currently works, not how you should override it.

It's really easy actually. Example:

$ cat main.c
#include <stdio.h>

extern int rt_init();
extern void d_func();
int main(int argc, char *argv[])
{
    printf("hello from C!\n");
    rt_init();
    printf("done initializing runtime\n");
    d_func();
}
$ cat dmain.d
extern(C) void d_func()
{
    import std.stdio;
    writeln("hello from D!");
}

shared static this()
{
    import std.stdio;
    writeln("doing some pre-run init!");
}
$ gcc -c main.c
$ dmd dmain.d main.o
$ ./dmain
hello from C!
doing some pre-run init!
done initializing runtime
hello from D!

-Steve

When I do this in my project and try to link in the lib or obj created from the C project, I get "entry point must be defined" error. I tried to add both to the "command line".

If I don't have a main function in D, I get all kinds of unresolved externals. Not sure why, but it seems to not import in the runtime when there is no main defined.

Basically I created a side C++ project, copied your code in to the main.cpp. I then added the obj file to the linker command line path. Then I get the missing entry point error. It is very similar to yours, just using VisualD and windows.

Seems like it would be easier just to create a wrapper process and use shared memory to transfer data between them ;/

For example, if I just create a new D project. Add a new cpp file and paste your C code in, paste the D code in the empty D file, and try to compile, I get:

main.cpp
main.obj : error LNK2019: unresolved external symbol "int __cdecl rt_init(void)" (?rt_init@@YAHXZ) referenced in function main main.obj : error LNK2019: unresolved external symbol "void __cdecl d_func(void)" (?d_func@@YAXXZ) referenced in function main CMain.obj : error LNK2001: unresolved external symbol _D14TypeInfo_Const6__vtblZ CMain.obj : error LNK2019: unresolved external symbol _d_throwc referenced in function _D3std9exception143__T12errnoEnforceTiVAyaa54_423a5c444c616e675c646d64325c77696e646f77735c62696e5c2e2e5c2e2e5c7372635c70686f626f735c7374645c737464696f2e64Vmi2543Z12errnoEnforceFNfiLAyaZi (@safe int std.exception.errnoEnforce!(int, "dmd2\windows\bin\..\..\src\phobos\std\stdio.d", 2543uL).errnoEnforce(int, lazy immutable(char)[])) CMain.obj : error LNK2001: unresolved external symbol _D12TypeInfo_Aya6__initZ CMain.obj : error LNK2019: unresolved external symbol _D3std3utf12isValidDcharFNaNbNiNfwZb referenced in function _D3std5stdio4File17LockingTextWriter10__T3putTwZ3putMFNbNiNfwZv CMain.obj : error LNK2019: unresolved external symbol _D3std9exception14ErrnoException6__ctorMFNeAyaAyamZC3std9exception14ErrnoException (@trusted std.exception.ErrnoException std.exception.ErrnoException.__ctor(immutable(char)[], immutable(char)[], ulong)) referenced in function _D3std9exception143__T12errnoEnforceTiVAyaa54_423a5c444c616e675c646d64325c77696e646f77735c62696e5c2e2e5c2e2e5c7372635c70686f626f735c7374645c737464696f2e64Vmi2543Z12errnoEnforceFNfiLAyaZi (@safe int std.exception.errnoEnforce!(int, "dmd2\windows\bin\..\..\src\phobos\std\stdio.d", 2543uL).errnoEnforce(int, lazy immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _aApplycd1 referenced in function _D3std5stdio4File17LockingTextWriter12__T3putTAyaZ3putMFNfAyaZv (@safe void std.stdio.File.LockingTextWriter.put!(immutable(char)[]).put(immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _D3std5stdio8__assertFiZv (void std.stdio.__assert(int)) referenced in function _D3std5stdio4File17LockingTextWriter10__T3putTwZ3putMFNbNiNfwZv CMain.obj : error LNK2019: unresolved external symbol _D3std5stdio4File17lockingTextWriterMFNfZS3std5stdio4File17LockingTextWriter (@safe std.stdio.File.LockingTextWriter std.stdio.File.lockingTextWriter()) referenced in function _D3std5stdio16__T7writelnTAyaZ7writelnFNfAyaZv (@safe void std.stdio.writeln!(immutable(char)[]).writeln(immutable(char)[])) CMain.obj : error LNK2001: unresolved external symbol _D3std5stdio12__ModuleInfoZ CMain.obj : error LNK2019: unresolved external symbol _D3std5stdio13trustedStdoutFNdNeZS3std5stdio4File (@property @trusted std.stdio.File std.stdio.trustedStdout()) referenced in function _D3std5stdio16__T7writelnTAyaZ7writelnFNfAyaZv (@safe void std.stdio.writeln!(immutable(char)[]).writeln(immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _D3std3utf6toUTF8FNaNbNiNfNkJG4awZAa referenced in function _D3std5stdio4File17LockingTextWriter10__T3putTwZ3putMFNbNiNfwZv CMain.obj : error LNK2019: unresolved external symbol _d_assert_msg referenced in function _D3std5stdio4File17LockingTextWriter12__T3putTAyaZ3putMFNfAyaZv (@safe void std.stdio.File.LockingTextWriter.put!(immutable(char)[]).put(immutable(char)[])) CMain.obj : error LNK2001: unresolved external symbol _D10TypeInfo_k6__initZ CMain.obj : error LNK2019: unresolved external symbol _D3std9exception14ErrnoException7__ClassZ referenced in function _D3std9exception143__T12errnoEnforceTiVAyaa54_423a5c444c616e675c646d64325c77696e646f77735c62696e5c2e2e5c2e2e5c7372635c70686f626f735c7374645c737464696f2e64Vmi2543Z12errnoEnforceFNfiLAyaZi (@safe int std.exception.errnoEnforce!(int, "dmd2\windows\bin\..\..\src\phobos\std\stdio.d", 2543uL).errnoEnforce(int, lazy immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _D3std5stdio4File6__dtorMFNfZv (@safe void std.stdio.File.__dtor()) referenced in function _D3std5stdio16__T7writelnTAyaZ7writelnFNfAyaZv (@safe void std.stdio.writeln!(immutable(char)[]).writeln(immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _d_newclass referenced in function _D3std9exception143__T12errnoEnforceTiVAyaa54_423a5c444c616e675c646d64325c77696e646f77735c62696e5c2e2e5c2e2e5c7372635c70686f626f735c7374645c737464696f2e64Vmi2543Z12errnoEnforceFNfiLAyaZi (@safe int std.exception.errnoEnforce!(int, "dmd2\windows\bin\..\..\src\phobos\std\stdio.d", 2543uL).errnoEnforce(int, lazy immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _D3std5stdio7__arrayZ referenced in function _D3std5stdio4File17LockingTextWriter10__T3putTwZ3putMFNbNiNfwZv CMain.obj : error LNK2019: unresolved external symbol _d_assert referenced in function _D4main8__assertFiZv (void main.__assert(int)) CMain.obj : error LNK2019: unresolved external symbol _d_unittest referenced in function _D4main15__unittest_failFiZv (void main.__unittest_fail(int)) CMain.obj : error LNK2019: unresolved external symbol _D3std5stdio4File17LockingTextWriter6__dtorMFNeZv (@trusted void std.stdio.File.LockingTextWriter.__dtor()) referenced in function _D3std5stdio16__T7writelnTAyaZ7writelnFNfAyaZv (@safe void std.stdio.writeln!(immutable(char)[]).writeln(immutable(char)[])) CMain.obj : error LNK2019: unresolved external symbol _d_arraybounds referenced in function _D4main7__arrayZ x64\Debug DMD\CMain.exe : fatal error LNK1120: 23 unresolved externals

So, something funky is going on. Any ideas?


Reply via email to