03-09-2016 07:09 AM
Hi!
I have some c-code in where I would like to use malloc to allocate memory but I got some error when I try to compile it.
C-code:
#include <stdio.h>
#include <stdlib.h>
void getAvg(double a[200000], double fs, double freq) {
int *array=malloc(16);
return;
}
Error code:
"14:02:59 **** Incremental Build of configuration Debug for project PommacTest ****
Info: Internal Builder is used for build
x86_64-nilrt-linux-gcc -O0 -g3 -Wall -c -fmessage-length=0 -o test.o "..\\test.c"
..\test.c: In function 'getAvg':
..\test.c:16:10: warning: unused variable 'array' [-Wunused-variable]
x86_64-nilrt-linux-gcc -shared -o libPommacTest.so test.o
c:/program files (x86)/national instruments/eclipse/14.0/x64/sysroots/i686-nilrtsdk-mingw32/usr/bin/x86_64-nilrt-linux/../../libexec/x86_64-nilrt-linux/gcc/x86_64-nilrt-linux/4.7.2/ld.exe: test.o: relocation R_X86_64_PC32 against undefined symbol `malloc@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
c:/program files (x86)/national instruments/eclipse/14.0/x64/sysroots/i686-nilrtsdk-mingw32/usr/bin/x86_64-nilrt-linux/../../libexec/x86_64-nilrt-linux/gcc/x86_64-nilrt-linux/4.7.2/ld.exe: final link failed: Bad value
collect2.exe: error: ld returned 1 exit status
14:02:59 Build Finished (took 295ms)"
Any idea?
/Anton
03-09-2016 09:00 AM
As it calls out in the error message, if you're compiling a loadable ELF without linking (the -c option), you should be passing -fPIC if you depend on functions defined in other libraries (in this case, malloc() comes from libc.so.6). -fPIC allows Position Independent Code which permits the use of other library's symbols in this library.