mpi compiling warning implicit declaration - c

today I install mpi on mi mac 10.9 with home-brew mpicc works with a simlpe hello world program, but if I try somethings like this
#include <mpi.h>
#include <string.h>
#include <stdio.h>
#define max 1000
int main(int argv, char *argc[]){
int myrank,nProc,tag,j;
char buff [max];
MPI_Status status;
tag=0;
MPI_Init(&argv,&argc);
MPI_Comm_Rank(MPI_COMM_WORLD,&myrank);
MPI_Comm_Size(MPI_COMM_WORLD,&nProc);
if(myrank==0){
for(j =1 ; j<nProc;j++){
MPI_Recv(&buff,max,MPI_CHAR,j,tag,MPI_COMM_WORLD,&status);
printf("Il processo %d dice di chiamarsi %s\n",j,buff);
}
}
else{
switch (myrank){
case 1 :
MPI_Send("Franco",max,MPI_CHAR,j,tag,MPI_COMM_WORLD);
break;
case 2 :
MPI_Send("Mena",max,MPI_CHAR,j,tag,MPI_COMM_WORLD);
break;
case 3 :
MPI_Send("Nino",max,MPI_CHAR,j,tag,MPI_COMM_WORLD);
break;
}
}
printf("Ciao da %d \n",myrank);
MPI_Finalize();
return(0);
}
and i try compiling it whit the following row:
mpicc -o filename filename.c
it give me this warnings and not built .
nucciampi.c:15:3: warning: implicit declaration of function 'MPI_Comm_Rank' is
invalid in C99 [-Wimplicit-function-declaration]
MPI_Comm_Rank(MPI_COMM_WORLD,&myrank);
^
nucciampi.c:16:3: warning: implicit declaration of function 'MPI_Comm_Size' is
invalid in C99 [-Wimplicit-function-declaration]
MPI_Comm_Size(MPI_COMM_WORLD,&nProc);
^
2 warnings generated.
Undefined symbols for architecture x86_64:
"_MPI_Comm_Rank", referenced from:
_main in nucciampi-zsehoq.o
"_MPI_Comm_Size", referenced from:
_main in nucciampi-zsehoq.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
please help me to solve this issues!!!

MPI_Comm_Rank -> MPI_Comm_rank
MPI_Comm_Size -> MPI_Comm_size

There is a well-defined naming convention for all routines and constants in MPI and it is described in §2.2 of the MPI specification:
In C, all routines associated with a particular type of MPI object should be of the form MPI_Class_action_subset or, if no subset exists, of the form MPI_Class_action. In Fortran, all routines associated with a particular type of MPI object should be of the form MPI_CLASS_ACTION_SUBSET or, if no subset exists, of the form MPI_CLASS_ACTION. For C and Fortran we use the C++ terminology to define the Class. In C++, the routine is a method on Class and is named MPI::Class::Action_subset. If the routine is associated with a certain class, but does not make sense as an object method, it is a static member function of the class.
Note that unlike in Fortran, symbol names in C are case sensitive.

Related

Undefined symbol "_clone" on OS X

Code:
#include <stdio.h>
#include <sched.h>
#include <stdlib.h>
#include <sys/wait.h>
#define _GNU_SOURCE
void *stack_memory()
{
const int stackSize = 65536;
void* stack = (void*)malloc(stackSize);
if (stack == NULL) {
printf("%s\n", "Cannot allocate memory \n");
exit(EXIT_FAILURE);
}
return stack;
}
int jail(void *args)
{
printf("Hello !! - child \n");
return EXIT_SUCCESS;
}
int main()
{
printf("%s\n", "Hello, world! - parent");
clone(jail, stack_memory(), SIGCHLD, 0);
return EXIT_SUCCESS;
}
Error:
Undefined symbols for architecture x86_64: "_clone", referenced
from:
_main in docker-4f3ae8.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code
1 (use -v to see invocation)
Linux doesn't prefix symbols with a leading _ so you're not using Linux.
But the clone(2) system call is Linux-specific, according to its man page.
clone() is Linux-specific and should not be used in programs intended
to be portable.
Probably you're using OS X or something. And you're compiling as C, so calling an un-declared function isn't a compile-time error (just a big warning). This is why it's a linker error instead of a compile-time error (and you ignored compiler warnings.)
And BTW, #define _GNU_SOURCE after including header files is pointless. You have to define feature-request macros before including headers to get them to define prototypes for GNU-only functions in cases where that's not already the default.

Compiling C programs with static files

I am trying to compile a c program with a static library and its not working .
This is the error :
undefined reference to `calculatearea'
collect2.exe: error: ld returned 1 exit status .
The static files were made with the gcc / g++ compilers .
This is the main code :
#include <stdio.h>
#include <stdint.h>
int calculatearea(int a , int b);
int main()
{
int c = calculatearea(2,4);
printf("%d",c);
getchar();
return 0;
}
edit :
: screenshot of compiler error
From the above code we can see that you have declared the function int calculatearea(int a , int b); but have not written any definition for the same. and you are calling this function in the main. compiler is not finding the definition for the function calculatearea and giving error.
To solve this:
1) Write the definition for function calculatearea in the same file.
2) Make use of extern specifier with this function declaration and make sure that definition is present with the link library at the time of compilation.
3) As mentioned in the picture if the area.o have the definition of function calculatearea, then compile as below, this will generate a.out in linux:
gcc filename.c area.o

Undefined symbols for architecture x86_64 and linker command failed with exit code 1

I run the following code from APUE
#include "apue.h"
#include <sys/wait.h>
void pr_exit(int status)
{
if (WIFEXITED(status))
printf("normal termination, exit status = %d\n",
WEXITSTATUS(status));
else if (WIFSIGNALED(status))
printf("abnormal termination, signal number = %d%s\n",
WTERMSIG(status),
#ifdef WCOREDUMP
WCOREDUMP(status) ? " (core file generated)" : "");
#else
"");
#endif
else if (WIFSTOPPED(status))
printf("child stopped, signal number = %d\n",
WSTOPSIG(status));
}
but get error:
$ cc my_wait.c
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I checked multiple times and ensure there no difference with the book's instruction ..
How could I solve the problem?
Transferring a comment into an answer, as requested.
The error message says "there is no function main()", and the source code you show has no function main(), so there's minimal surprise that there's that error message.
Where did you think main() was going to come from?
When you build a program, there needs to be a main() from somewhere, and the standard C library does not provide an implementation. (If you work with Flex or Lex, or Bison or Yacc, you may find minimal main() programs in their libraries, but these are an exception, not the rule.)

Error: L6218E: Undefined symbol three()

[Edit: The question is flawed, the file I described as "main.c" was actually "main.cpp" and that it why I was having an issue, calling a C function from a C++ file. The question is thus incorrect and doesn't have an answer, but if you have this undefined symbol issue, also think about checking you're not mixing C & C++.]
I'm using uVision 5 to develop a firmware, however I can't get the linker to find one of my functions.
main.c :
#include "Test.h"
int main()
{
return three();
}
Test.h :
#ifndef TEST_H
#define TEST_H
int three();
#endif
Test.c
#include "Test.h"
int three()
{
return 3;
}
All those files are at the root of my project, I know they get compiled as if I introduce a syntax error in them, compiler reports an error.
Also looking at the map file produced, I see that three() was removed:
Removing test.o(i.three), (4 bytes).
For testing purposes, I had --no_remove to linker command line, map file now contains:
0x0002ba76 0x00000004 Code RO 1 i.three test.o
So obviously, the linker is well aware of my function, and will or won't remove it depending on flags.
Regardless, it reports:
.\build\uvision5\test.axf: Error: L6218E: Undefined symbol three() (referred from main.o).
Not enough information to list image symbols.
Flawed question, it was actually a case of mixing C/C++, in which case you'll get a symbol missing if you call a C function from C++ without declaring it extern C.

How to run this threads example?

I'm trying the code from Tanenbaum's 3e "Modern Operating Systems" and I get compiler errors and warnings:
$ LANG=en_US.UTF-8 cc thread.c
thread.c: In function ‘main’:
thread.c:19:63: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
thread.c:25:1: warning: passing argument 1 of ‘exit’ makes integer from pointer without a cast [enabled by default]
/usr/include/stdlib.h:544:13: note: expected ‘int’ but argument is of type ‘void *’
/tmp/ccqxmMgE.o: In function `main':
thread.c:(.text+0x57): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
This is the code I'm trying
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUMBER_OF_THREADS 10
void *print_hello_world(void *tid)
{
//printf("Hello World. Greetings from thread %d0", tid);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUMBER_OF_THREADS];
int status, i;
for(i=0; i<NUMBER_OF_THREADS; i++) {
//printf("Main here creating thread %d0", i);
status = pthread_create(&threads[i], NULL, print_hello_world, (void *)i);
if (status != 0) {
//printf("Oops. pthread_create returned error code %d0", status);
exit(-1);
}
}
exit(NULL);
}
Can you help me improve the state of the code so that it will run? There appears to be some errata since the exact code from the book doesn't compile. Thanks
Please link to pthread library, by specifying -lpthread option to your linker.
Also, you should be using pthread_join to wait for all the created threads to complete.
$gcc thread.c -lpthread
This is to link the pthread shared library.
1) You have to link to libpthread to get rid of the linker error:
gcc ..... -lpthread
(note that the -lpthread option must be the last one)!
2) exit(NULL); is wrong; NULL is for pointer types whereas exit wants an int to be supplied; use simply
exit(0);
instead.
The other warnings are just system-dependent pointer and integer size warnings; they can safely be ignored in most cases.
Pl. do not use the exit statement in your main function in this case, since the main may get exited and your threads also will terminate and you may not get the outputs of the print statement in the thread function.
Pl. use pthread_exit instead of exit in main so that even your main thread terminates the other threads can continue.

Resources