exaloop / codon

A high-performance, zero-overhead, extensible Python compiler using LLVM
https://docs.exaloop.io/codon
Other
15.01k stars 517 forks source link

Cannot reproduce C/C++ interop from docs #342

Closed ProkopHapala closed 1 year ago

ProkopHapala commented 1 year ago

I'm trying to follow this documentation page: https://docs.exaloop.io/codon/interoperability/cpp

but when I run following

#!/bin/bash
codon build -o libcodon_lib.so codon_lib.py
gcc -o c_program -L. -lcodon_lib c_program.c
./c_program

it complains it is not compiled with -fPIC and I don't know how to pass -fPIC flag into codon:

prokophapala@prokophapala-Lenovo-ideapad-Y700-15ISK:~/Dropbox/MyDevSW/codon/C_interop$ ./compile_and_run.sh
/usr/bin/ld: libcodon_lib.so.o: relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: failed to set dynamic section sizes: bad value
collect2: error: ld returned 1 exit status
error: process for 'gcc' exited with status 1
/usr/bin/ld: cannot find -lcodon_lib: No such file or directory
collect2: error: ld returned 1 exit status
./compile_and_run.sh: line 9: ./c_program: No such file or directory

So I tried to compile .a rather than .so:

#!/bin/bash
codon build -o libcodon_lib.a codon_lib.py
gcc -o c_program -L. -lcodon_lib c_program.c
./c_program

But that does not work either (note I have always problem how to set lineker properly, that's why I use CMake if possible):

/usr/bin/ld: ./libcodon_lib.a: in function `_start':
(.text+0x0): multiple definition of `_start'; /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o:(.text+0x0): first defined here
/usr/bin/ld: ./libcodon_lib.a: in function `_fini':
(.fini+0x0): multiple definition of `_fini'; /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
/usr/bin/ld: ./libcodon_lib.a:(.rodata+0x0): multiple definition of `_IO_stdin_used'; /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o:(.rodata.cst4+0x0): first defined here
/usr/bin/ld: ./libcodon_lib.a: in function `data_start':
(.data+0x0): multiple definition of `__data_start'; /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o:(.data+0x0): first defined here
/usr/bin/ld: ./libcodon_lib.a: in function `data_start':
(.data+0x8): multiple definition of `__dso_handle'; /usr/lib/gcc/x86_64-linux-gnu/11/crtbeginS.o:(.data.rel.local+0x0): first defined here
/usr/bin/ld: ./libcodon_lib.a: in function `_init':
(.init+0x0): multiple definition of `_init'; /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/bin/ld: /tmp/ccTwy6hl.o: in function `main':
c_program.c:(.text+0x0): multiple definition of `main'; ./libcodon_lib.a:(.text+0x4f90): first defined here
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/crtendS.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'; ./libcodon_lib.a:(.data+0x10): first defined here
/usr/bin/ld: cannot use executable file './libcodon_lib.a' as input to a link
collect2: error: ld returned 1 exit status
./compile_and_run.sh: line 9: ./c_program: No such file or directory
qjhuang commented 1 year ago

use codon build --relocation-model=pic --lib -o libmycodon.so my_codon.pyx , it works for me on debian bulleye

ProkopHapala commented 1 year ago

@qjhuang Thanks, there is some progress, with this script I got:

codon build --relocation-model=pic --lib -o libcodon_lib.so codon_lib.py

wd=`pwd`
echo $wd
export LD_LIBRARY_PATH=$wd  

gcc -o c_program -L. -lcodon_lib c_program.c
/usr/bin/ld: /tmp/ccBl2NF1.o: in function `main':
c_program.c:(.text+0xe): undefined reference to `foo'
collect2: error: ld returned 1 exit status
./compile_and_run_cleaned.sh: line 11: ./c_program: No such file or directory
ProkopHapala commented 1 year ago

Aha, this stupid gcc it wan't the library as the last argument. This works fine:

#!/bin/bash

codon build --relocation-model=pic --lib -o libcodon_lib.so codon_lib.py

wd=`pwd`
echo $wd
export LD_LIBRARY_PATH=$wd  

gcc -o c_program -L.  c_program.c -lcodon_lib
elisbyberi commented 1 year ago

@ProkopHapala While it seems that the problem has been solved, it is worth noting for future reference that this is a duplicate of issue #108.

cool-dev-guy commented 1 year ago

i get this error

/usr/bin/ld: /tmp/ccq0M1RC.o: in function `main': test.cc:(.text+0xe): undefined reference to `foo(long)' collect2: error: ld returned 1 exit status

elisbyberi commented 1 year ago

@cool-dev-guy Following instructions in #108 I was able to compile it. Especially running the command in terminal like this: LD_LIBRARY_PATH=${HOME}/software/exaloop/codon/codon-0.15.1/lib/codon:. ./foo

cool-dev-guy commented 1 year ago

Thanks for your help

inumanag commented 1 year ago

Glad that it worked. Closing it—please reopen if there are further issues.