471,310 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

errors with definition of main program

5
Hello everybody,
I am getting bogged-down by these errors troubling me for the last few days
I am writing a C program with a main and several user-defined functions. One of the user-defined functions calls a Fortran 90 subroutine.
The main definition is as follows

void selfenerg(real, int); /*declaration of function selfenerg*/

int main (int argc, char **argv)
{ ..................
....................
...................
prompt_file = stdout;
in_file = stdin;
out_file = stdout;

if(argc > 1) { /* command line: a.out, or a.out in, or a.out in out */
prompt_file = stderr;
in_file = fopen(argv[1], "r");
assert(in_file != NULL);
}
if(argc > 2) {
out_file = fopen(argv[2], "w");
assert(out_file != NULL);
}
.............................................
............................................
selfenerg(parameters);
.............................
.............................
/*with the user-defined functions*/

void surfGF4_(double*, double*, double*, double*, double*, double*, double*, double*, double*); /* fortran 90 subroutine declaration */

void selfenerg(real d_omega, int len)
{ ........................................
..........................................
...................
surfGF4_(&w, &sigm11[0][0], &sigm12[0][0], &sigm21[0][0], &sigm22[0][0], &sigmN_1N_1[0][0], &sigmN_1N[0][0], &sigmNN_1[0][0], &sigmNN[0][0]);
/*calling fortran 90 function*/
............................................
.............................................


subroutine surfGF4(w, sigm11, sigm12, sigm21, sigm22, sigmN_1N_1, sigmN_1N, sigmNN_1, sigmNN)

implicit none
.................................................. ....
.................................................. ....

I compile these main c subroutine and fotran 90 programs using Linux commands

pgf90 -c surfGF4.f90
icc -c GlobalRevo.c .../.../....(extension to handle fast fourier transform)
pgf90 surfGF4.o GlobalRevo.o ...../...../.(extension to handle fast fourier transform)

sometimes I remove the extension from the second line
I also try to use ifort instead of pgf90, but in either case I encounter the following errors

GlobalRevo.o: In function `main':
GlobalRevo.c:(.text+0x0): multiple definition of `main'
/share/intel/9.1-x86_64/fce/9.1.051/lib/for_main.o:: first defined here
ld: Warning: size of symbol `main' changed from 70 in /share/intel/9.1-x86_64/fce/9.1.051/lib/for_main.o to 2354 in GlobalRevo.o
/share/intel/9.1-x86_64/fce/9.1.051/lib/for_main.o: In function `main':
: undefined reference to `MAIN__'
GlobalRevo.o: In function `retardselfenerg':
GlobalRevo.c:(.text+0x419d): undefined reference to `surfGF4_'
GlobalRevo.c:(.text+0x4449): undefined reference to `surfGF4_'
GlobalRevo.c:(.text+0x46f5): undefined reference to `surfGF4_'
GlobalRevo.c:(.text+0x49a1): undefined reference to `surfGF4_'
GlobalRevo.c:(.text+0x4c4d): undefined reference to `surfGF4_'
GlobalRevo.o:GlobalRevo.c:(.text+0x4ef9): more undefined references to `surfGF4_' follow

So, the major errors listed here include multiple definition of 'main', undefined reference to 'main' and undefined reference to 'surfGF4_'(the fortran 90 subroutine that is called by the main c program)

I have not fully understood a main of type
int main (int argc, char **argv), especially the two arguments shown here
I try to change this main header to the simpler form like
int main (void)
but it does not help either
Anyone can help me address the errors here?
Thanks
Aug 1 '07 #1
4 2917
weaknessforcats
9,208 Expert Mod 8TB
You can use:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.  
  4. }
  5.  
or
Expand|Select|Wrap|Line Numbers
  1. int main (int argc, char **argv)
  2. {
  3.  
  4. }
  5.  
depending upon whether you use argc or argv.

However, this error:

GlobalRevo.c:(.text+0x0): multiple definition of `main'

says there are multiple main functions in GlobalRevo.c.

The surfGF4 function must be in a library. Do you have the Fortran library with this function in your make file??

I can't tell from your code where main ends exactly.
Aug 1 '07 #2
urkel
5
You can use:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.  
  4. }
  5.  
or
Expand|Select|Wrap|Line Numbers
  1. int main (int argc, char **argv)
  2. {
  3.  
  4. }
  5.  
depending upon whether you use argc or argv.

However, this error:

GlobalRevo.c:(.text+0x0): multiple definition of `main'

says there are multiple main functions in GlobalRevo.c.

The surfGF4 function must be in a library. Do you have the Fortran library with this function in your make file??

I can't tell from your code where main ends exactly.

The main program ends after the 2 dashed-lines before /* with the user defined function*/
fortran 90 subroutine 'surfGF4' is my own fortran code, it uses no fortran library functions at all
I have checked that all the user-defined functions have been called at least once from the main program. The reason is, I am worried if one or more of these user-defined functions is not called by the main at least once, then the compiler will think that the function is just another main. Is it a correct surmise?
I am concerned if a user defined function in C should not call another user-defined function/subroutine just as 'selfenerg' does here. If indeed it shouldn't, the failure of 'selfenerg' to call 'surfGF4' may be not surprising and also since they are written in different languages. Does it seem a plausible explanation and what may be the possible solution?

Thanks
Aug 1 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
There is a documented standard for the calling conventions in C. If I remember, C uses the same conventions as Fortran. Therefore, youcan call a Fortran function in C.

To do that you need a) a header file with the C function protoype, b) a library containing the the compiled code for the Fortran library.

Do you have that lilbrary? If so, it needs to be added to your make as an additional linker dependency.

The reason is, I am worried if one or more of these user-defined functions is not called by the main at least once, then the compiler will think that the function is just another main. Is it a correct surmise?
No. main is known as main. You can have only one.

Funcitons are called by name.

The compiler never sees these functions. All it sees is the function prototype.
The compiler just notes that the function is an "unresolved external reference" but does not produce an error. It is the job of the linker to locate that function.

If indeed it shouldn't, the failure of 'selfenerg' to call 'surfGF4' may be not surprising and also since they are written in different languages.
Who cares. Once a function is compiled to binary the language it was written in is irrelevant.

Again, The C side needs a C function prototype and a binary of the compiled function. This is usually supplied as a library. I may not be correct, but I believe you can use a Fortran library as input to the C linker.

This code:
subroutine surfGF4(w, sigm11, sigm12, sigm21, sigm22, sigmN_1N_1, sigmN_1N, sigmNN_1, sigmNN)
looks different on the C side. w and sigm11 are addresses. yes?? Because the C call is looking for double*.
Aug 1 '07 #4
Hello everybody,
I am getting bogged-down by these errors troubling me for the last few days
I am writing a C program with a main and several user-defined functions. One of the user-defined functions calls a Fortran 90 subroutine.
The main definition is as follows

void selfenerg(real, int); /*declaration of function selfenerg*/

int main (int argc, char **argv)
{ ..................
....................
...................
prompt_file = stdout;
in_file = stdin;
out_file = stdout;

if(argc > 1) { /* command line: a.out, or a.out in, or a.out in out */
prompt_file = stderr;
in_file = fopen(argv[1], "r");
assert(in_file != NULL);
}
if(argc > 2) {
out_file = fopen(argv[2], "w");
assert(out_file != NULL);
}
.............................................
............................................
selfenerg(parameters);
.............................
.............................
/*with the user-defined functions*/

void surfGF4_(double*, double*, double*, double*, double*, double*, double*, double*, double*); /* fortran 90 subroutine declaration */

void selfenerg(real d_omega, int len)
{ ........................................
..........................................
...................
surfGF4_(&w, &sigm11[0][0], &sigm12[0][0], &sigm21[0][0], &sigm22[0][0], &sigmN_1N_1[0][0], &sigmN_1N[0][0], &sigmNN_1[0][0], &sigmNN[0][0]);
/*calling fortran 90 function*/
............................................
.............................................


subroutine surfGF4(w, sigm11, sigm12, sigm21, sigm22, sigmN_1N_1, sigmN_1N, sigmNN_1, sigmNN)

implicit none
.................................................. ....
.................................................. ....

I compile these main c subroutine and fotran 90 programs using Linux commands

pgf90 -c surfGF4.f90
icc -c GlobalRevo.c .../.../....(extension to handle fast fourier transform)
pgf90 surfGF4.o GlobalRevo.o ...../...../.(extension to handle fast fourier transform)

sometimes I remove the extension from the second line
I also try to use ifort instead of pgf90, but in either case I encounter the following errors

GlobalRevo.o: In function `main':
GlobalRevo.c:(.text+0x0): multiple definition of `main'
/share/intel/9.1-x86_64/fce/9.1.051/lib/for_main.o:: first defined here
ld: Warning: size of symbol `main' changed from 70 in /share/intel/9.1-x86_64/fce/9.1.051/lib/for_main.o to 2354 in GlobalRevo.o
/share/intel/9.1-x86_64/fce/9.1.051/lib/for_main.o: In function `main':
: undefined reference to `MAIN__'
GlobalRevo.o: In function `retardselfenerg':
GlobalRevo.c:(.text+0x419d): undefined reference to `surfGF4_'
GlobalRevo.c:(.text+0x4449): undefined reference to `surfGF4_'
GlobalRevo.c:(.text+0x46f5): undefined reference to `surfGF4_'
GlobalRevo.c:(.text+0x49a1): undefined reference to `surfGF4_'
GlobalRevo.c:(.text+0x4c4d): undefined reference to `surfGF4_'
GlobalRevo.o:GlobalRevo.c:(.text+0x4ef9): more undefined references to `surfGF4_' follow

So, the major errors listed here include multiple definition of 'main', undefined reference to 'main' and undefined reference to 'surfGF4_'(the fortran 90 subroutine that is called by the main c program)

I have not fully understood a main of type
int main (int argc, char **argv), especially the two arguments shown here
I try to change this main header to the simpler form like
int main (void)
but it does not help either
Anyone can help me address the errors here?
Thanks
Put the statement return 0;
before closing the main.

You must have one and only one main() function in your program
Even though you keep them in different files; if you include another
file which also contains main then it will be a conflict to the compiler from
where to start with.
Avoid such things and have only one main() function.

Regards,
Girish.
Aug 2 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Thomi Richards | last post: by
30 posts views Thread by prasanna | last post: by
24 posts views Thread by Massimo Soricetti | last post: by
2 posts views Thread by errmaker | last post: by
1 post views Thread by dasilva109 | last post: by
2 posts views Thread by Andrew | last post: by
reply views Thread by RK | last post: by
4 posts views Thread by Dan | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.