简体   繁体   中英

C program Compilation error : reference to main

I am new to C programming and need help in resolving the compilation issue :

There are 3 .c files (main.c, file1.c , file2.c) file1.c and file2.c just contain the function definitions that are called in main.c. When I am trying to compile my main.c file using the below command, it gives me the errors:

 gcc -Wall ./trigger-solve/main.c

  _approx_help", referenced from:
  _print_help in main-9e2c6e.o
  "_approximate", referenced from:
  _main in main-9e2c6e.o
  "_free_matrix", referenced from:
  _main in main-9e2c6e.o
  "_print_matrix", referenced from:
  _main in main-9e2c6e.o
  "_read_matrix", referenced from:
  _main in main-9e2c6e.o
  "_read_sparse_matrix", referenced from:
  _main in main-9e2c6e.o 
  ld: symbol(s) not found for architecture x86_64
   clang: error: linker command failed with exit code 1 (use -v to see invocation)

However after reading about this error , I figured out its happening because file1.c and file2.c do not contain the main function . So I added this main function in both files :

   int main()
   {
     return 1;
    }

At this point when I compile file1.c and file2.c they DO NOT give me errors , however I am still getting the same compilation errors while running main.c. Also I don't know after compiling file1.c and file2.c how should I link them to main.c ?

Can someone please help .

You need to link all your translation units together. Perhaps all in one wash, like this:

gcc -Wall main.c file1.c file2.c

Now you have an executable file called a.out ready for your enjoyment.

Of course rebuilding the entire project every time you change one file isn't feasible, so normally you'd compile each TU separately and then link everything in a final step (and give the output file a better name):

gcc -Wall -c main.c
gcc -Wall -c file1.c
gcc -Wall -c file2.c

gcc -o myprog main.o file1.o file2.o

Did you do any research yourself on this? There are plenty of relevant examples available online.

You should not define main function in file1.c and file2.c. You should compile all .c files with -c option and then link them to main like this :

gcc main.o file1.o file2.o

you can just

  gcc -Wall *.c

will compile all .c files in the current directory... thats a good shortcut if you save your each of your project in a separate folder

Basically, you have got the linking error. Remember, any program has only one main() function. In your example, this main() must be finally linked with the other files to create an executable. The main() is the entry point of any C program. Thus, there should be only one main() in your source code in order to create an executable. For your case, I will say use the Makefile for compiling and linking. The makefile saves time of compilation and is quite handy.

I will say - use the Makefile. The Makefile makes the job easier. In future, if you have to extend the functionality, add a new file and add a new entry in the makefile. This is the simplest form of makefile here.

Once you issue make - the executable name - app will be created. Next, run the ./app

Makefile

target = file1.0 file2.o main.o 
[tab] gcc file1.o file2.o main.o -o app
file1.o: file1.c
[tab] gcc -c file1.c -o file1.o
file2.o: file2.c
[tab] gcc -c file2.c -o file2.o
main.o: main.c
[tab] gcc -c main.c -o main.o

Now, run the Makefile.

$make target

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM