Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Malloc in c-code on RT

Hi!

 

I run some c-code on my cDAQ and using eclipse to compile it. However i would like to use the function malloc in the c-code but I get some error when compiling.

#include <stdio.h>
#include <stdlib.h>


void getAvg(double a[200000], double fs, double freq) {
	  int *array=malloc(16);
	  return;
}

When compiling the C-code above I get the error  from eclipse:

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)

Is it possible to use malloc and if so what might be the problem causing the error?

 

 

 

0 Kudos
Message 1 of 5
(5,046 Views)

That error has nothing to do with the use of malloc(). Technically while you do assign a malloced memory buffer to that variable, it is afterwards never used and that is what the C compiler warns you about. Notice that it is a warning and not an error.

 

GCC has many optimization passes going through its Internal Representation (IR) graph after the C code has been translated into the IR. One of those optimization pases is to recognize if a variable is never used in its scope lifetime. Just assigning a value to a variable is in that sense not a use of a variable as it makes technically not much sense to assign something to a variable and never use it.

 

And if you would pass this code through Valgrind it would also complain about the fact that you created a memory leak which is in fact a real error, but the GCC code analyzer and optimizer is not meant to catch such runtime errors.

 

The actual error which fails your build is that when creating shared library object files and calling external functions such as C runtime functions, you need to compile your code in Generate position-independent code (PIC) mode, and you do that by passing the -fPIC command line option to the GCC command when compiling your C code into object files. In Eclipse that is somewhere in the C compiler options.

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 5
(5,030 Views)

It was not the warning i meant but the error 

error: ld returned 1 exit status  

The also I can see that no .so file is present after compiling. 

0 Kudos
Message 3 of 5
(5,019 Views)

I extended my original answer but you may not have seen it. You need to compile the C code files with the -fPIC option when wanting to make shared libraries that call into other shared libraries. And malloc() being a C runtime library function is in libc.so.

 

Just read the ld error message and google for it.

Rolf Kalbermatter
My Blog
0 Kudos
Message 4 of 5
(5,014 Views)

-fPIC solved the problem, tanks for your help!

0 Kudos
Message 5 of 5
(4,854 Views)