Re: Linker cannot find malloc and free on OS X

2017-06-05 Thread Jacob Carlborg via Digitalmars-d-learn

On 2017-06-05 13:48, bvoq wrote:


So I ran: dmd -unittest -main -v -L-lgmp -L-lc -g gmp/*
The error seems to stem from: cc dbgio.o -o dbgio -g -m64 -Xlinker
-no_compact_unwind -lgmp -lc -L/usr/local/Cellar/dmd/2.074.0/lib -lgmp
-lgmp -lgmp -lgmp -lc -lphobos2 -lpthread -lm

Full invocation of command with verbose flag: cc dbgio.o -o dbgio -g
-m64 -Xlinker -no_compact_unwind -lgmp -lc
-L/usr/local/Cellar/dmd/2.074.0/lib -lgmp -lgmp -lgmp -lgmp -lc
-lphobos2 -lpthread -lm -v
gmp -lc -L/usr/local/Cellar/dmd/2.074.0/lib -lgmp -lgmp -lgmp -lgmp -lc
-lphobos2 -lpthread -lm -v
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin


"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld"
-demangle -lto_library
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib
-dynamic -arch x86_64 -macosx_version_min 10.12.0 -o dbgio
-L/usr/local/Cellar/dmd/2.074.0/lib dbgio.o -no_compact_unwind -lgmp -lc
-lgmp -lgmp -lgmp -lgmp -lc -lphobos2 -lpthread -lm -lSystem
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.1.0/lib/darwin/libclang_rt.osx.a

Undefined symbols for architecture x86_64:
  "free", referenced from:
  _D3gmp1z3MpZ6__ctorMFNaNbNcNiNexAyakZS3gmp1z3MpZ in dbgio.o
  _D3gmp1z3MpZ10fromStringMFNaNbNcNiNjNexAyakZS3gmp1z3MpZ in dbgio.o


I've managed to isolate and identified the problem. The problem is these 
two lines [1]:


pragma(mangle, "malloc") void* qualifiedMalloc(size_t size);
pragma(mangle, "free") void qualifiedFree(void* ptr);

On macOS all C symbols are mangled with an underscore prefix, meaning 
the "free" and "malloc" symbols are actually named _free and _malloc in 
the binary.


This is easy to verify by:

$ cat main.c
void foo();

int main()
{
foo();
return 0;
}

$ clang main.c
Undefined symbols for architecture x86_64:
  "_foo", referenced from:
  _main in main-8a6861.o

Here we can see that the linker is looking for the "_foo" symbol, while 
in your output it's looking for "free", without the underscore prefix.


When pragma(mangle) is used on an extern(C) symbol the compiler will 
automatically handle the prefixing of the symbol on macOS, making it 
line up with any C symbols.


Functions with D linkage have their own mangling, prefixed with _D, the 
fully qualified name mangled and the signature. It seems like that when 
using pragma(mangle) on function with D linkage the compiler will output 
the symbol exactly like it's specified in the source code, in 
pragma(mangle). This can be verified by:


$ cat main.d
void foo();

void main()
{
foo();
}

$ dmd -c main.d
$ nm main.o | grep foo
U _D4main3fooFZv

And when using pragma(mangle):

$ cat main.d
pragma(mangle, "foo") void foo();

void main()
{
foo();
}

$ dmd -c main.d
$ nm main.o | grep foo
U foo

No underscore prefix.

This happens to work on Linux because on Linux this mangling (with the 
underscore prefix) is not used for C symbols. But since the functions 
are not declared as extern(C) we're actually calling C functions using 
the D calling conventions, which just happens to work in this case 
because the C and D calling conventions are mostly the same.


[1] https://github.com/nordlow/gmp-d/blob/master/src/gmp/z.d#L1164-L1165

--
/Jacob Carlborg


Re: Linker cannot find malloc and free on OS X

2017-06-05 Thread bvoq via Digitalmars-d-learn

On Monday, 5 June 2017 at 10:34:12 UTC, Jacob Carlborg wrote:

On 2017-06-05 01:14, bvoq wrote:


The flag -L-lc seems to have been passed to the library.
This is the full error message after running it with dub test 
--verbose


You need to continue to invoke the sub commands, that is, DMD, 
Clang and the linker with the verbose flag (-v) added. There's 
no point in looking for "-lc" since the C standard library on 
macOS is placed in /usr/lib/libSystem.B.dylib.


So I ran: dmd -unittest -main -v -L-lgmp -L-lc -g gmp/*
The error seems to stem from: cc dbgio.o -o dbgio -g -m64 
-Xlinker -no_compact_unwind -lgmp -lc 
-L/usr/local/Cellar/dmd/2.074.0/lib -lgmp -lgmp -lgmp -lgmp -lc 
-lphobos2 -lpthread -lm


Full invocation of command with verbose flag: cc dbgio.o -o dbgio 
-g -m64 -Xlinker -no_compact_unwind -lgmp -lc 
-L/usr/local/Cellar/dmd/2.074.0/lib -lgmp -lgmp -lgmp -lgmp -lc 
-lphobos2 -lpthread -lm -v
gmp -lc -L/usr/local/Cellar/dmd/2.074.0/lib -lgmp -lgmp -lgmp 
-lgmp -lc -lphobos2 -lpthread -lm -v

Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o dbgio -L/usr/local/Cellar/dmd/2.074.0/lib dbgio.o -no_compact_unwind -lgmp -lc -lgmp -lgmp -lgmp -lgmp -lc -lphobos2 -lpthread -lm -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.1.0/lib/darwin/libclang_rt.osx.a

Undefined symbols for architecture x86_64:
  "free", referenced from:
  _D3gmp1z3MpZ6__ctorMFNaNbNcNiNexAyakZS3gmp1z3MpZ in dbgio.o
  _D3gmp1z3MpZ10fromStringMFNaNbNcNiNjNexAyakZS3gmp1z3MpZ in 
dbgio.o
 (maybe you meant: 
_D2rt4util9container5treap33__T5TreapTS2gc11gcinterface4RootZ5Treap8freeNodeFNbNiPS2rt4util9container5treap33__T5TreapTS2gc11gcinterface4RootZ5Treap4NodeZv, _D2gc4impl12conservative2gc3Gcx8log_freeMFNbPvZv , _D2gc4impl12conservative2gc4Pool12freePageBitsMFNbmKxG4mZv , _D2gc4impl12conservative2gc8freeTimel , _D2rt5minfo11ModuleGroup4freeMFZv , _D2gc4impl6manual2gc8ManualGC4freeMFNbPvZv , _D2gc4impl12conservative2gc14ConservativeGC10freeNoSyncMFNbPvZv , _D2gc4impl12conservative2gc14ConservativeGC163__T9runLockedS63_D2gc4impl12conservative2gc14ConservativeGC10freeNoSyncMFNbPvZvS37_D2gc4impl12conservative2gc8freeTimelS37_D2gc4impl12conservative2gc8numFreeslTPvZ9runLockedMFNbKPvZv , _D2rt4util9container5treap34__T5TreapTS2gc11gcinterface5RangeZ5Treap8freeNodeFNbNiPS2rt4util9container5treap34__T5TreapTS2gc11gcinterface5RangeZ5Treap4NodeZv , _D2gc4impl12conservative2gc15LargeObjectPool9freePagesMFNbmmZv , _D2rt7dwarfeh15ExceptionHeader4freeFPS2rt7dwarfeh15ExceptionHeaderZv , _D2gc4!

impl12conservative2gc14ConservativeGC4freeMFNbPvZv , 
_D4core6memory2GC4freeFNaNbPvZv , _gc_free )
  "malloc", referenced from:
  _D3gmp1z3MpZ19_allocStringzCopyOfMFNaNbNiNexAyaZPa in 
dbgio.o
 (maybe you meant: 
_D2gc4impl12conservative2gc3Gcx10log_mallocMFNbPvmZv, 
_D2gc4impl12conservative2gc10mallocTimel , 
_D2gc4impl6manual2gc8ManualGC6mallocMFNbmkxC8TypeInfoZPv , 
_D2gc4impl12conservative2gc14ConservativeGC207__T9runLockedS83_D2gc4impl12conservative2gc14ConservativeGC13reallocNoSyncMFNbPvmKkKmxC8TypeInfoZPvS40_D2gc4impl12conservative2gc10mallocTimelS40_D2gc4impl12conservative2gc10numMallocslTPvTmTkTmTxC8TypeInfoZ9runLockedMFNbKPvKmKkKmKxC8TypeInfoZPv , _D2gc4impl12conservative2gc14ConservativeGC6mallocMFNbmkxC8TypeInfoZPv , _D2gc4impl12conservative2gc14ConservativeGC12mallocNoSyncMFNbmkKmxC8TypeInfoZPv , _D4core6memory2GC6mallocFNaNbmkxC8TypeInfoZPv , _gc_malloc , _D2rt4util9container6common7xmallocFNbNimZPv , _D2gc4impl12conservative2gc14ConservativeGC200__T9runLockedS79_D2gc4impl12conservative2gc14ConservativeGC12mallocNoSyncMFNbmkKmxC8TypeInfoZPvS40_D2gc4impl12conservative2gc10mallocTimelS40_D2gc4impl12conservative2gc10numMallocslTmTkTmTxC8TypeInfoZ9runLockedMFNbKmKkKmKxC8TypeInfoZPv )

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to 
see invocation)



--

From there the command which yielded the error is:

"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld"
 -demangle -lto_library 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib
 -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o dbgio 
-L/usr/local/Cellar/dmd/2.074.0/lib dbgio.o -no_compact_unwind -lgmp -lc -lgmp -lgmp 
-lgmp -lgmp -lc -lphobos2 -lpthread -lm -lSystem 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.1.0/lib/darwin/libclang_rt.osx.a
 -v



The 

Re: Linker cannot find malloc and free on OS X

2017-06-05 Thread Jacob Carlborg via Digitalmars-d-learn

On 2017-06-05 01:14, bvoq wrote:


The flag -L-lc seems to have been passed to the library.
This is the full error message after running it with dub test --verbose


You need to continue to invoke the sub commands, that is, DMD, Clang and 
the linker with the verbose flag (-v) added. There's no point in looking 
for "-lc" since the C standard library on macOS is placed in 
/usr/lib/libSystem.B.dylib.


--
/Jacob Carlborg


Re: Linker cannot find malloc and free on OS X

2017-06-04 Thread bvoq via Digitalmars-d-learn

On Sunday, 4 June 2017 at 16:13:22 UTC, Jacob Carlborg wrote:

On 2017-06-04 12:45, Nordlöw wrote:

My gmp-d tests successfully on Linux as

dub test

but on OS X it fails as

Undefined symbols for architecture x86_64:
  "free", referenced from:
  ...
  "malloc", referenced from:
  ...

Any ideas on why?

https://github.com/nordlow/gmp-d/issues/4#issuecomment-305974761


I would recommend adding the --verbose flag to see the exact 
commands invoked. That will print how the D compiler was 
invoked. The copy-paste the exact command and add the -v flag 
(for verbose). To the same with the C compiler and the linker.


The flag -L-lc seems to have been passed to the library.
This is the full error message after running it with dub test 
--verbose



Using dub registry url 'http://code.dlang.org/'
Refreshing local packages (refresh existing: true)...
Looking for local package map at 
/var/lib/dub/packages/local-packages.json
Looking for local package map at 
/Users/kdkdk/.dub/packages/local-packages.json
Determined package version using GIT: gmp-d 
0.0.3+commit.16.gedb1291

Refreshing local packages (refresh existing: false)...
Looking for local package map at 
/var/lib/dub/packages/local-packages.json
Looking for local package map at 
/Users/kdkdk/.dub/packages/local-packages.json

  Found dependency libgmp 1.0.0
Refreshing local packages (refresh existing: false)...
Looking for local package map at 
/var/lib/dub/packages/local-packages.json
Looking for local package map at 
/Users/kdkdk/.dub/packages/local-packages.json

  Found dependency libgmp 1.0.0
Checking for upgrades.
Using cached upgrade results...
Generating test runner configuration 'gmp-d-test-library' for 
'library' (library).
Get module name from path: 
/Users/kdkdk/Documents/psi/gmp-d/src/gmp/dbgio.d
Get module name from path: 
/Users/kdkdk/Documents/psi/gmp-d/src/gmp/f.d
Excluding package.d file from test due to 
https://issues.dlang.org/show_bug.cgi?id=11847
Get module name from path: 
/Users/kdkdk/Documents/psi/gmp-d/src/gmp/q.d
Get module name from path: 
/Users/kdkdk/Documents/psi/gmp-d/src/gmp/traits.d
Get module name from path: 
/Users/kdkdk/Documents/psi/gmp-d/src/gmp/z.d

Refreshing local packages (refresh existing: false)...
Looking for local package map at 
/var/lib/dub/packages/local-packages.json
Looking for local package map at 
/Users/kdkdk/.dub/packages/local-packages.json

  Found dependency libgmp 1.0.0
Generate target gmp-d (executable 
/Users/kdkdk/Documents/psi/gmp-d gmp-d-test-library)
Generate target libgmp (staticLibrary 
/Users/kdkdk/.dub/packages/libgmp-1.0.0/libgmp libgmp)

Performing "unittest" build using dmd for x86_64.
libgmp 1.0.0: target for configuration "library" is up to date.
Using existing build in 
/Users/kdkdk/.dub/packages/libgmp-1.0.0/libgmp/.dub/build/library-unittest-posix.osx-x86_64-dmd_2074-0CBFB25E65B46672F7CAF7CC5BABCE87/.
Copying target from 
/Users/kdkdk/.dub/packages/libgmp-1.0.0/libgmp/.dub/build/library-unittest-posix.osx-x86_64-dmd_2074-0CBFB25E65B46672F7CAF7CC5BABCE87/liblibgmp.a to /Users/kdkdk/.dub/packages/libgmp-1.0.0/libgmp
Target 
'/Users/kdkdk/Documents/psi/gmp-d/.dub/build/gmp-d-test-library-unittest-posix.osx-x86_64-dmd_2074-6A4F59B4FBEFFE587575F03F4A8B5BD0/gmp-d-test-library' doesn't exist, need rebuild.
gmp-d 0.0.3+commit.16.gedb1291: building configuration 
"gmp-d-test-library"...

Using direct -l... flags for gmp, c.
dmd -c 
-of.dub/build/gmp-d-test-library-unittest-posix.osx-x86_64-dmd_2074-6A4F59B4FBEFFE587575F03F4A8B5BD0/gmp-d-test-library.o -debug -g -unittest -w -version=VibeCustomMain -version=Have_gmp_d -version=Have_libgmp -Isrc/ -I../../../.dub/packages/libgmp-1.0.0/libgmp/source/ ../../../../../var/folders/7s/rl2b7bj92t7c2dlnnmgzmc20gn/T/dub_test_root-94ea7d66-86c6-4a27-9ede-42b0d74e6604.d src/gmp/dbgio.d src/gmp/f.d src/gmp/package.d src/gmp/q.d src/gmp/traits.d src/gmp/z.d -vcolumns

Linking...
dmd 
-of.dub/build/gmp-d-test-library-unittest-posix.osx-x86_64-dmd_2074-6A4F59B4FBEFFE587575F03F4A8B5BD0/gmp-d-test-library .dub/build/gmp-d-test-library-unittest-posix.osx-x86_64-dmd_2074-6A4F59B4FBEFFE587575F03F4A8B5BD0/gmp-d-test-library.o ../../../.dub/packages/libgmp-1.0.0/libgmp/.dub/build/library-unittest-posix.osx-x86_64-dmd_2074-0CBFB25E65B46672F7CAF7CC5BABCE87/liblibgmp.a -L-lgmp -L-lc -g

Undefined symbols for architecture x86_64:
  "free", referenced from:
  _D3gmp1z3MpZ6__ctorMFNaNbNcNiNexAyakZS3gmp1z3MpZ in 
gmp-d-test-library.o
  _D3gmp1z3MpZ10fromStringMFNaNbNcNiNjNexAyakZS3gmp1z3MpZ in 
gmp-d-test-library.o
 (maybe you meant: 
_D2rt4util9container5treap33__T5TreapTS2gc11gcinterface4RootZ5Treap8freeNodeFNbNiPS2rt4util9container5treap33__T5TreapTS2gc11gcinterface4RootZ5Treap4NodeZv, _D2gc4impl12conservative2gc3Gcx8log_freeMFNbPvZv , _D2gc4impl12conservative2gc15LargeObjectPool9freePagesMFNbmmZv , _D4core6memory2GC4freeFNaNbPvZv , 

Re: Linker cannot find malloc and free on OS X

2017-06-04 Thread Jacob Carlborg via Digitalmars-d-learn

On 2017-06-04 12:45, Nordlöw wrote:

My gmp-d tests successfully on Linux as

dub test

but on OS X it fails as

Undefined symbols for architecture x86_64:
  "free", referenced from:
  ...
  "malloc", referenced from:
  ...

Any ideas on why?

https://github.com/nordlow/gmp-d/issues/4#issuecomment-305974761


I would recommend adding the --verbose flag to see the exact commands 
invoked. That will print how the D compiler was invoked. The copy-paste 
the exact command and add the -v flag (for verbose). To the same with 
the C compiler and the linker.


--
/Jacob Carlborg


Re: Linker cannot find malloc and free on OS X

2017-06-04 Thread Stefan Koch via Digitalmars-d-learn

On Sunday, 4 June 2017 at 10:45:23 UTC, Nordlöw wrote:

My gmp-d tests successfully on Linux as

dub test

but on OS X it fails as

Undefined symbols for architecture x86_64:
  "free", referenced from:
  ...
  "malloc", referenced from:
  ...

Any ideas on why?

https://github.com/nordlow/gmp-d/issues/4#issuecomment-305974761


try giving it -lc