unfamous
unfamous

Reputation: 611

compile .c with gcc

I'm trying to compile a program that have main.c and a lot of .c and .h files . Is there any way to compile and link without passing all .c file in the gcc command like

gcc  main.c file.c file2.c -o main

Upvotes: 1

Views: 335

Answers (5)

jpmuc
jpmuc

Reputation: 1154

As already said, make is the best way to go Learn just what you need at every point

Besides, it is important to also use some flags that will help you out while coding:

-Wall -> sets all warning flags
-g, -ggdb -> generates debug code
-ansi, -std=c99

A really good book on how to use make is, http://shop.oreilly.com/product/9780937175903.do

Upvotes: 0

John
John

Reputation: 476

'make' is the tool for building C apps. Below is the hello world version.

$ cat main.c 
#include <stdio.h>

int main (char *argv[], int argc) {
    printf("Hello World\n"); 
    return 0; 
}
$ make main
cc     main.c   -o main
$ ./main 
Hello World

Edited in deference to Shahbaz comment: The original question was trying to simplify the command-line for gcc. The right direction for the programmer is to learn about make. Since there is a bit of a learning curve with make, I wanted to offer simple stepping stone which does something useful. By getting started in this way, you don't need a make file. Simply type 'make programname' as shown above. Make uses its default rules and associated varabiles. $(CC) -c $(CFLAGS) $(CPPFLAGS) The astute programmer can build on this by setting well-know variables.

From here one can tinker with a makefile. In the spirit of stepping stones, consider this trival makefile

$ cat makefile 

SRCS = main.c
OBJ = ${SRCS:.c=.o}
CC=gcc

main: ${OBJ}

$ make
gcc   main.o   -o main

By setting the well-known make variable CC to control which compiler is used. OBJ is computed from the list of source files. The trival target starts one on the road to rules.

Anyway, my hope is this post and other answers get the original questioner on there way.

Regards, -jk

Upvotes: 3

Man of One Way
Man of One Way

Reputation: 3980

gcc -o main -I. `find . -name "*\.c"`

This way you could have the .c and .h files in subfolders if you wish.

However, this is not a good way of doing it. A better way would be to create a Makefile

Upvotes: 0

Larry OBrien
Larry OBrien

Reputation: 8606

If you're asking about the command-line, you can just use wildcards to specify all the .c files as @Man of One Way suggested. But in general, C applications of any-but-trivial-size are built using "makefiles" which are extremely helpful.

You might want to read a tutorial such as http://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/makefile.html

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 993085

Your shell can expand wildcards. So you can:

gcc *.c -o main

Of course, you'll have to make sure that you don't have any extra *.c files in the directory that you don't actually want compiled. A better option is to use a build system such as Make or SCons.

Upvotes: 6

Related Questions