Thread overview
Linking g++ compiled object files
Nov 23, 2016
Rubikoid
Nov 24, 2016
Basile B.
Nov 24, 2016
Rubikoid
Nov 24, 2016
Mike Parker
November 23, 2016
For example, i have test.cpp:
#include <stdio.h>
void test()
{
   printf("test\n");
}
And test.d:
import std.stdio;
extern (C++) void test();
void main()
{
    test();
    readln();
}
How i should compile test.cpp using g++ to link it normally?
November 24, 2016
On Wednesday, 23 November 2016 at 19:49:35 UTC, Rubikoid wrote:
> For example, i have test.cpp:
> #include <stdio.h>
> void test()
> {
>    printf("test\n");
> }
> And test.d:
> import std.stdio;
> extern (C++) void test();
> void main()
> {
>     test();
>     readln();
> }
> How i should compile test.cpp using g++ to link it normally?

* Under linux:
  - create the object
    g++ -c test.cpp
  - link:
    dmd test.d test.o

* Under windows 32 bit:
 - create the object:
    Use digital mars C/C++ (dmc) to create an OMF object (GCC would produce COFF) then
 - link:
    dmd test.d test.obj

Note that in both cases it can be better not to use the same name for the cpp object.
With more C++ objects you could create an archive (aka static library) but for just one source this is useless.
November 24, 2016
On Thursday, 24 November 2016 at 17:37:51 UTC, Basile B. wrote:
> * Under windows 32 bit:
>  - create the object:
>     Use digital mars C/C++ (dmc) to create an OMF object (GCC would produce COFF) then
>  - link:
>     dmd test.d test.obj
>
So, is there any way to use gcc/g++ under windows?
> Note that in both cases it can be better not to use the same name for the cpp object.
My mistake, already understood;)


November 24, 2016
On Thursday, 24 November 2016 at 20:09:29 UTC, Rubikoid wrote:
> So, is there any way to use gcc/g++ under windows?


DMD can work with COFF objects when given -m32mscoff when compiling 32-bit and -m64 for 64-bit. In both cases, yoi will need the Microsoft linker and SDK intalled. However, when linking with GCC-compiled objects from MinGW or Cygwin, any use of the C standard library in those objects is bound to cause linker errors. With C++, there's also the issue of name mangling. I don't know if GCC's and Microsoft's are compatible.