www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Building application with LDC and -flto=thin fails in link stage

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
When I try build my application using LDC and -flto=thin it fails 
in the final linking as

ldmd2 
-of.dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-9FDE475789CA2E324E9DAE
A959C2B7F/knetquery .dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-9FDE475789CA2E324E9DAE6A
59C2B7F/knetquery.o ../../.dub/packages/gmp-d-master/gmp-d/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-0CE08DAF93E48445402E07C
537FA4E6/libgmp-d.a phobos-next/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-933571CC843E8D13926CEC14051E7195/l
bknet_phobos-next.a -L--no-as-needed -L-lzstd -L-lgmp -L-lc -L-lreadline -L-lz
-L-lbz2
/usr/bin/ld: error: 
.dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-9FDE475789CA2E324E9DAE6A959C2
7F/knetquery.o:1:3: invalid character
/usr/bin/ld: error: 
.dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-9FDE475789CA2E324E9DAE6A959C2
7F/knetquery.o:1:3: syntax error, unexpected $end
/usr/bin/ld: error: 
.dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-9FDE475789CA2E324E9DAE6A9
9C2B7F/knetquery.o: not an object or archive
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o:function
_start: error: undefined reference to 'main'
collect2: error: ld returned 1 exit status
Error: /usr/bin/gcc failed with status: 1
FAIL 
.dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-9FDE475789CA2
324E9DAE6A959C2B7F/ knetquery executable
ldmd2 failed with exit code 1.

What could be wrong?
Mar 26 2018
next sibling parent reply Johan Engelen <j j.nl> writes:
On Monday, 26 March 2018 at 22:07:49 UTC, Nordlöw wrote:
 When I try build my application using LDC and -flto=thin it 
 fails in the final linking
You must also pass `-flto=thin` during linking (a special plugin is needed for LTO, and LDC will only pass the plugin to the linker when `-flto=` is specified). I couldn't see `-flto=thin` in your link command, so I suspect that will fix it. - Johan
Mar 26 2018
parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Monday, 26 March 2018 at 22:13:02 UTC, Johan Engelen wrote:
 On Monday, 26 March 2018 at 22:07:49 UTC, Nordlöw wrote:
 When I try build my application using LDC and -flto=thin it 
 fails in the final linking
You must also pass `-flto=thin` during linking (a special plugin is needed for LTO, and LDC will only pass the plugin to the linker when `-flto=` is specified). I couldn't see `-flto=thin` in your link command, so I suspect that will fix it. - Johan
I added `lflags` to my dub configuration as buildType "release-nobounds-lto" { buildOptions "releaseMode" "optimize" "inline" dflags "-mcpu=native" "-O3" "-flto=thin" platform="posix-ldc" lflags "-flto=thin" platform="posix-ldc" } forwarded as `-L-flto=thin` but still errors as Linking... ldmd2 -of.dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C B0B23442E/knetquery .dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C2B B23442E/knetquery.o ../../.dub/packages/gmp-d-master/gmp-d/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-B287F67CE5FF6145BC22979 CFB09607/libgmp-d.a phobos-next/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F0F2FDB01B8401C04D657BCC145D46A5/l bknet_phobos-next.a -L--no-as-needed -L-flto=thin -L-lzstd -L-lgmp -L-lc -L-lreadline -L-lz -L-lbz2 /usr/bin/ld: error: .dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C2B0B234 2E/knetquery.o:1:3: invalid character /usr/bin/ld: error: .dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C2B0B234 2E/knetquery.o:1:3: syntax error, unexpected $end /usr/bin/ld: error: .dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C2B0 23442E/knetquery.o: not an object or archive /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o:function _start: error: undefined reference to 'main' collect2: error: ld returned 1 exit status Error: /usr/bin/gcc failed with status: 1 FAIL .dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA2 7C077E5C2B0B23442E/ knetquery executable ldmd2 failed with exit code 1.
Mar 26 2018
parent reply kinke <kinke libero.it> writes:
On Monday, 26 March 2018 at 23:32:59 UTC, Nordlöw wrote:
 forwarded as `-L-flto=thin` but still errors as
Which is wrong, it's not a ld command-line option (i.e., the `-L` prefix is wrong). I don't use dub though, so I don't know how to fix it.
Mar 27 2018
parent reply Johan Engelen <j j.nl> writes:
On Tuesday, 27 March 2018 at 13:28:08 UTC, kinke wrote:
 On Monday, 26 March 2018 at 23:32:59 UTC, Nordlöw wrote:
 forwarded as `-L-flto=thin` but still errors as
Which is wrong, it's not a ld command-line option (i.e., the `-L` prefix is wrong).
Indeed. Please try to manually link first (without dub) by modifying the command on which dub errors: ``` ldmd2 -flto=thin -of.dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C B0B23442E/knetquery .dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C2B B23442E/knetquery.o ../../.dub/packages/gmp-d-master/gmp-d/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-B287F67CE5FF6145BC22979 CFB09607/libgmp-d.a phobos-next/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F0F2FDB01B8401C04D657BCC145D46A5/l bknet_phobos-next.a -L--no-as-needed -L-lzstd -L-lgmp -L-lc -L-lreadline -L-lz -L-lbz2 ``` -Johan
Mar 27 2018
parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Tuesday, 27 March 2018 at 22:00:42 UTC, Johan Engelen wrote:
 Indeed.
 Please try to manually link first (without dub) by modifying 
 the command on which dub errors:

 ```
 ldmd2 -flto=thin 
 -of.dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C
B0B23442E/knetquery .dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C2B
B23442E/knetquery.o ../../.dub/packages/gmp-d-master/gmp-d/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-B287F67CE5FF6145BC22979
CFB09607/libgmp-d.a phobos-next/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F0F2FDB01B8401C04D657BCC145D46A5/l
bknet_phobos-next.a -L--no-as-needed -L-lzstd -L-lgmp -L-lc -L-lreadline -L-lz
-L-lbz2
 ```

 -Johan
Yes, that works!
Mar 27 2018
parent reply Johan Engelen <j j.nl> writes:
On Tuesday, 27 March 2018 at 22:10:33 UTC, Per Nordlöw wrote:
 On Tuesday, 27 March 2018 at 22:00:42 UTC, Johan Engelen wrote:
 Indeed.
 Please try to manually link first (without dub) by modifying 
 the command on which dub errors:

 ```
 ldmd2 -flto=thin 
 -of.dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C
B0B23442E/knetquery .dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C2B
B23442E/knetquery.o ../../.dub/packages/gmp-d-master/gmp-d/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-B287F67CE5FF6145BC22979
CFB09607/libgmp-d.a phobos-next/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F0F2FDB01B8401C04D657BCC145D46A5/l
bknet_phobos-next.a -L--no-as-needed -L-lzstd -L-lgmp -L-lc -L-lreadline -L-lz
-L-lbz2
 ```

 -Johan
Yes, that works!
Good :-) I'm no dub expert and I don't know how to pass flags to the _compiler_ for the link step. Would be good to figure that out with the dub folks. There will be similar problems with using ASan (and fuzzer aswell): `-fsanitize=address` must also be passed to the D compiler (not the linker) during linking such that the correct asan library is linked into the executable. - Johan
Mar 28 2018
next sibling parent reply Seb <seb wilzba.ch> writes:
On Wednesday, 28 March 2018 at 16:42:23 UTC, Johan Engelen wrote:
 On Tuesday, 27 March 2018 at 22:10:33 UTC, Per Nordlöw wrote:
 On Tuesday, 27 March 2018 at 22:00:42 UTC, Johan Engelen wrote:
 Indeed.
 Please try to manually link first (without dub) by modifying 
 the command on which dub errors:

 ```
 ldmd2 -flto=thin 
 -of.dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C
B0B23442E/knetquery .dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C2B
B23442E/knetquery.o ../../.dub/packages/gmp-d-master/gmp-d/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-B287F67CE5FF6145BC22979
CFB09607/libgmp-d.a phobos-next/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F0F2FDB01B8401C04D657BCC145D46A5/l
bknet_phobos-next.a -L--no-as-needed -L-lzstd -L-lgmp -L-lc -L-lreadline -L-lz
-L-lbz2
 ```

 -Johan
Yes, that works!
Good :-) I'm no dub expert and I don't know how to pass flags to the _compiler_ for the link step. Would be good to figure that out with the dub folks. There will be similar problems with using ASan (and fuzzer aswell): `-fsanitize=address` must also be passed to the D compiler (not the linker) during linking such that the correct asan library is linked into the executable. - Johan
dub supports dflags and lflags in the config file. lflags are the linker commands. http://code.dlang.org/package-format?lang=sdl#build-settings You can also specify per platform arguments, e.g. dflags "-version=BuildingWithLDCRocks" platform="ldc" (dub even supports the environment variables DFLAGS which dmd doesn't (and of course LFLAGS))
Mar 28 2018
parent reply Johan Engelen <j j.nl> writes:
On Wednesday, 28 March 2018 at 17:03:07 UTC, Seb wrote:
 
 dub supports dflags and lflags in the config file. lflags are 
 the linker commands.
Please read the thread. `lflags` is for passing flags to the _linker_ (i.e. those flags are prefixed with -L when passed to the _compiler_) Here, what's needed is passing flags to the _compiler_ when it is invoked to perform the link step in the build. -Johan
Mar 28 2018
parent reply Jacob Carlborg <doob me.com> writes:
On Wednesday, 28 March 2018 at 20:09:24 UTC, Johan Engelen wrote:
 On Wednesday, 28 March 2018 at 17:03:07 UTC, Seb wrote:
 
 dub supports dflags and lflags in the config file. lflags are 
 the linker commands.
Please read the thread. `lflags` is for passing flags to the _linker_ (i.e. those flags are prefixed with -L when passed to the _compiler_) Here, what's needed is passing flags to the _compiler_ when it is invoked to perform the link step in the build.
Please read the reply :), although it could be a bit more clear. `dflags` is used to pass arbitrary flags to the compiler. See the bottom of the table at [1]. [1] https://code.dlang.org/package-format?lang=sdl#build-settings -- /Jacob Carlborg
Mar 29 2018
parent Johan Engelen <j j.nl> writes:
On Thursday, 29 March 2018 at 08:44:21 UTC, Jacob Carlborg wrote:
 Please read the reply :), although it could be a bit more clear.
I'll spell it out for you. Both `dflags` and `lflags` are being used already. With separate compilation and linking, there seems to be no way to pass flags to the compiler during the linking step: dflags is not used and lflags is prefixed with `-L`. -Johan
Mar 30 2018
prev sibling parent Johan Engelen <j j.nl> writes:
On Wednesday, 28 March 2018 at 16:42:23 UTC, Johan Engelen wrote:
 On Tuesday, 27 March 2018 at 22:10:33 UTC, Per Nordlöw wrote:
 On Tuesday, 27 March 2018 at 22:00:42 UTC, Johan Engelen wrote:
 Indeed.
 Please try to manually link first (without dub) by modifying 
 the command on which dub errors:

 ```
 ldmd2 -flto=thin 
 -of.dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C
B0B23442E/knetquery .dub/build/application-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F2904BE3C4DA237C077E5C2B
B23442E/knetquery.o ../../.dub/packages/gmp-d-master/gmp-d/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-B287F67CE5FF6145BC22979
CFB09607/libgmp-d.a phobos-next/.dub/build/library-release-nobounds-lto-linux.posix-x86_64-ldc_2078-F0F2FDB01B8401C04D657BCC145D46A5/l
bknet_phobos-next.a -L--no-as-needed -L-lzstd -L-lgmp -L-lc -L-lreadline -L-lz
-L-lbz2
 ```

 -Johan
Yes, that works!
I'm no dub expert and I don't know how to pass flags to the _compiler_ for the link step. Would be good to figure that out with the dub folks.
For a stopgap solution: I think what you are doing is passing `--compiler=<yourpath>/ldmd2` (note: LDMD) to dub when building, and you get separate compilation+linking. If you'd use `--compiler=<yourpath>/ldc2` (note: LDC), then you would not get separate compilation+linking [1] and things work with just setting `dflags`. I've asked about the problem here: https://github.com/dlang/dub/issues/1431 cheers, Johan [1] https://github.com/dlang/dub/issues/809
Mar 30 2018
prev sibling parent Arek <arychlinski gmail.com> writes:
On Monday, 26 March 2018 at 22:07:49 UTC, Nordlöw wrote:
 When I try build my application using LDC and -flto=thin it 
 fails in the final linking as
According to LDC's Release info: Known issues: ThinLTO may not work well with the ld.bfd linker, use ld.gold instead (-linker=gold). Maybe this is the problem. Arek
Mar 27 2018