When I link my MPI C program, I get the following error:
undefined reference to MPI_recv.
What should i do to solve this error?
It is MPI_Recv, not MPI_recv.
You need to link to the MPI library that provides the MPI_recv() function. For example, with gcc, you would add an option like -lmpi to your compile line.
MPI implementations usually provide a compilation utility scripts that basically sets up the correct compilation & linking environment, libraries and calls gcc (e.g.) with the correct command line.
Try this:
mpicc my_prog.c
Hope this helps.
A.
Related
I included math.h library in my source code. Then, I'm compelled to write this to compile:
clang file.c -lm
to use functions of the library as pow(), when I could simply type:
make init
Otherwise compiler return an error as "pow()" not defined. How can I automatize this task at least in order to type less?
I use VS Code 1.66 in Fedora 35.
Thanks in advance.
I am trying to compile a hello world program in C on a Linux 64-bit machine. I am using an ARM cross compiler to load my application onto an ARM processor. However, when compiling the code using arm-none-eabi-gcc -o hello hello.c I get a series of errors:
/home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-exit.o): In function exit':
exit.c:(.text.exit+0x2c): undefined reference to_exit'
/home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-sbrkr.o): In function _sbrk_r':
sbrkr.c:(.text._sbrk_r+0x18): undefined reference to_sbrk'
/home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-writer.o): In function _write_r':
writer.c:(.text._write_r+0x20): undefined reference to_write'
/home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-closer.o): In function _close_r':
closer.c:(.text._close_r+0x18): undefined reference to_close'
/home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-fstatr.o): In function _fstat_r':
fstatr.c:(.text._fstat_r+0x1c): undefined reference to_fstat'
/home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-isattyr.o): In function _isatty_r':
isattyr.c:(.text._isatty_r+0x18): undefined reference to_isatty'
/home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-lseekr.o): In function _lseek_r':
lseekr.c:(.text._lseek_r+0x20): undefined reference to_lseek'
/home/dico/gcc-arm-none-eabi-4_7-2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/libc.a(lib_a-readr.o): In function _read_r':
readr.c:(.text._read_r+0x20): undefined reference to_read'
collect2: error: ld returned 1 exit status
When I try compiling by doing: arm-none-eabi-gcc -c hello.c, it creates an object code hello.o which tells me that the compiler runs fine.
Could someone perhaps tell me why my compilation is return such errors please?
UPDATE
I realize now that the C runtime library isn't included in the compilation. Does anyone know of any options I need to include in the compilation or how to link the library to be able to use standard functions such as printf for example?
What should the MCU chip do with a printf string since there is no console? That's what is happening. libc needs low level functions that is platform dependent and shall not be included in the libc.
You need a retarget.c or a syscalls.c file that defines all these missing symbols and properly retarget the printf output to a serial line that you have access from the outside of the chip.
I strongly recommend you to use the yagarto toolkit, they provide a syscall.c file with what's necessary to build a project when linking against libc, as you are doing.
If you want to try the short path, include the syscall.c file to your project and reprogram its functions internals to suit your needs.
Linking bare gcc toolchain against libc in the microcontroller world is not for beginners. My recommendation is always to follow step by step yagarto's tutorials.
We have installed GCC and libgcc on AIX.
I am busy compiling a C library on the machine and keep getting the following error:
Undefined symbol: .main
Any idea how to solve this?
Thanks for the help
Lynton
You are most likely using the wrong gcc arguments - probably you are using the same ones you'd use for an executable program (where main() is required). The correct arguments depend on what kind of library you want to build (a static one or a dynamic (.so) one)
I had written a small thread program when i compiled cc filename.c, i got some statements during compilation, but when i compiled using -lpthread (cc filename.c -lpthread) it got executed what is this -lpthread why is it required? can anyone explain this in detail. it would be of great help.
The pthread_create() function that you use in your program is not a basic C function, and requires that you use a library.
This is why you have to use this command switch -lpthread.
This gcc command tells him to look for a library named libpthread somewhere on your disk, and use it to provide the thread creation mechanisms.
I suggest you read this to get familiar with the "library" concept: http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
The -l option is typically used to specify a library (in this case, the pthread library) that should be linked with your program.
Since the thread functions often live in a separate library, you need an option like this when building a program that uses them, or you will get linker errors.
pthread is something called POSIX Threads. It's the standard library for threads in Unix-like POSIX envirnoments.
Since you are going to use pthread you need to tell the compiler to link to that library.
You can read more about exactly what lpthread is and how it works: https://computing.llnl.gov/tutorials/pthreads/
On compiling I am getting an error "undefined reference to pthread_create()" and similarly for "undefined reference to pthread_join()". What are the possible reasons? I am unable to identify them.
Are you sure you remembered the -lpthread flag in compilation? Usually that's the source of this error.
You should add -pthread, which adds the required flags for the preprocessor as well as the linker. The flag suggested by others, -lpthread, only links the library, which may lead to the system libraries not having proper threading support.