digitalmars.D.learn - Help learning how to interface with c(++)
- Kai Meyer (27/27) Mar 04 2011 I can't seem to get this to work right:
- Kagamin (2/22) Mar 05 2011 try non-shared libTest, dmd prefers single executable compilations.
- Kai Meyer (3/25) Mar 07 2011 Is that the only option? I know it's possible to link to external c
- Kai Meyer (11/38) Mar 07 2011 So I monkeyed around a little bit, and found out that if I change
- Kagamin (2/15) Mar 08 2011 Because you compile .cpp file, gcc compiles it with g++ as C++ and mangl...
I can't seem to get this to work right: gcc -m32 -shared -fPIC Test.cpp -o libTest.so g++ -m32 test_c.cpp -L. -lTest -o test_c wine htod.exe Test.h Test.d dmd test_d.d Test.d -L-L. -L-lTest -oftest_d test_d.o: In function `_Dmain': Test.d:(.text._Dmain+0x20): undefined reference to `increment' collect2: ld returned 1 exit status --- errorlevel 1 make: *** [test_d] Error 1 The resulting test_c binary from g++ works as intented (With either LD_LIBRARY_PATH="." or LD_RUN_PATH="."): $ ./test_c Count = 0 Count = 1 $ ldd test_c linux-gate.so.1 => (0x00ad1000) libTest.so (0x005b9000) libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x4970f000) libm.so.6 => /lib/libm.so.6 (0x4955b000) libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x49587000) libc.so.6 => /lib/libc.so.6 (0x493ab000) /lib/ld-linux.so.2 (0x4938a000) Any ideas on what I'm doing wrong here? I've dropbox-ed the code if it's helpful. http://dl.dropbox.com/u/12135920/kai_test_c_interface.zip -Kai Meyer
Mar 04 2011
Kai Meyer Wrote:I can't seem to get this to work right: gcc -m32 -shared -fPIC Test.cpp -o libTest.so g++ -m32 test_c.cpp -L. -lTest -o test_c wine htod.exe Test.h Test.d dmd test_d.d Test.d -L-L. -L-lTest -oftest_d test_d.o: In function `_Dmain': Test.d:(.text._Dmain+0x20): undefined reference to `increment' collect2: ld returned 1 exit status --- errorlevel 1 make: *** [test_d] Error 1 The resulting test_c binary from g++ works as intented (With either LD_LIBRARY_PATH="." or LD_RUN_PATH="."): $ ./test_c Count = 0 Count = 1 $ ldd test_c linux-gate.so.1 => (0x00ad1000) libTest.so (0x005b9000)try non-shared libTest, dmd prefers single executable compilations.
Mar 05 2011
On 03/05/2011 06:24 AM, Kagamin wrote:Kai Meyer Wrote:Is that the only option? I know it's possible to link to external c libraries, and I'd like to learn how.I can't seem to get this to work right: gcc -m32 -shared -fPIC Test.cpp -o libTest.so g++ -m32 test_c.cpp -L. -lTest -o test_c wine htod.exe Test.h Test.d dmd test_d.d Test.d -L-L. -L-lTest -oftest_d test_d.o: In function `_Dmain': Test.d:(.text._Dmain+0x20): undefined reference to `increment' collect2: ld returned 1 exit status --- errorlevel 1 make: *** [test_d] Error 1 The resulting test_c binary from g++ works as intented (With either LD_LIBRARY_PATH="." or LD_RUN_PATH="."): $ ./test_c Count = 0 Count = 1 $ ldd test_c linux-gate.so.1 => (0x00ad1000) libTest.so (0x005b9000)try non-shared libTest, dmd prefers single executable compilations.
Mar 07 2011
On 03/07/2011 08:57 AM, Kai Meyer wrote:On 03/05/2011 06:24 AM, Kagamin wrote:So I monkeyed around a little bit, and found out that if I change "extern (C)" to "extern (C++)", the library links correctly and the program runs. That lead me to believe that if I added the "-cpp" option to htod.exe, it would generate extern(C++) functions. But I got a blank Test.d file doing it that way. So, gcc created a libTest.so with a function in it, and dmd only finds the function with extern(C++) and not with extern(C). Now I'm just confused, yet pleased something worked. -Kai MeyerKai Meyer Wrote:Is that the only option? I know it's possible to link to external c libraries, and I'd like to learn how.I can't seem to get this to work right: gcc -m32 -shared -fPIC Test.cpp -o libTest.so g++ -m32 test_c.cpp -L. -lTest -o test_c wine htod.exe Test.h Test.d dmd test_d.d Test.d -L-L. -L-lTest -oftest_d test_d.o: In function `_Dmain': Test.d:(.text._Dmain+0x20): undefined reference to `increment' collect2: ld returned 1 exit status --- errorlevel 1 make: *** [test_d] Error 1 The resulting test_c binary from g++ works as intented (With either LD_LIBRARY_PATH="." or LD_RUN_PATH="."): $ ./test_c Count = 0 Count = 1 $ ldd test_c linux-gate.so.1 => (0x00ad1000) libTest.so (0x005b9000)try non-shared libTest, dmd prefers single executable compilations.
Mar 07 2011
Kai Meyer Wrote:gcc -m32 -shared -fPIC Test.cpp -o libTest.soSo I monkeyed around a little bit, and found out that if I change "extern (C)" to "extern (C++)", the library links correctly and the program runs. That lead me to believe that if I added the "-cpp" option to htod.exe, it would generate extern(C++) functions. But I got a blank Test.d file doing it that way. So, gcc created a libTest.so with a function in it, and dmd only finds the function with extern(C++) and not with extern(C). Now I'm just confused, yet pleased something worked.Because you compile .cpp file, gcc compiles it with g++ as C++ and mangles functions accordingly. You can tell gcc to compile the file as C.
Mar 08 2011