2

I have made two files in c i.e. file1.c file2.c. In file1.c I wrote

#include< stdio.h >                           
 int s=10;                                                                        
 void main()         
 {    
   printf("This is file 1");    
 }

In file2.c

include < stdio.h >                                                             
 extern int s;                                                                
 void main()    {                                                                         
    printf("%d",s);                                                                      
}

When I compiled file2.c in ubuntu terminal I got undefined referenced to s error. How can I resolve this error?

2
  • 1) the signature for main() ALWAYS returns int, not void 2) there can be only 1 function named main() 3) have one of those files (the one that finally contains the main() function, call the (new function name) in the second file AND provide a prototype for that (new function name) in the file that contains the main() function. Commented Apr 24, 2017 at 17:33
  • you seem to have a misunderstanding about 'compiling' and 'linking' The compiler will NOT complain about the s variable, HOWEVER, the linker will complain, Commented Apr 24, 2017 at 17:34

2 Answers 2

3

In the second case,

  extern int s; 

tells the compiler that "somewhere there" exists a variable s which has type int, but it actually does not "define" the variable. So, the linker has no clue where to find the variable, it cannot find the variable and throws the error.

You need to have a definition of the variable, either in a separate translation unit (the purpose of using extern) or in the same translation unit (if you want).

Sign up to request clarification or add additional context in comments.

Comments

1

In file1.c

#include <stdio.h>

void myfunction( void );

int s=10;
void myfunction()
{
    printf("This is file 1");
}

In file2.c

#include <stdio.h>

void myfunction( void );
extern int s;

int main( void )
{
    myfunction();
    printf("%d",s);
}

then compile (the example uses gcc

gcc -g -Wall -Wextra -pedantic -Wconversion -std=gnu11 -c file1.c -o file1.o
gcc -g -Wall -Wextra -pedantic -Wconversion -std=gnu11 -c file2.c -o file2.o

then link using:

gcc -g file1.o file2.o -o myexec

then run it as

./myexec

Of course, if your using Visual Studio, the command line statements will be slightly different

2 Comments

Thanks @user3629249 for your response. It is working fine. But I want to just know is there any way where it can work for two main function
to have more that one main() function means more than one executable program. then need to be able to communicate between those separate programs. There are several methods. The easiest would be to run the first program, have it fork() and exec..() the second program. (the second program gets a copy of the data defined in the first program). There are other methods like shared memory (depreciated), mmap(), pipes, etc etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.