Question:
GCC compiler- how to link two .c files?
2009-08-28 14:21:59 UTC
i know how to compile now in a command window:

"gcc file.c"

But now if i want to have a project with more than just one .c file, how can i make just ONE executable file?

How do i link?
Three answers:
The Phlebob
2009-08-28 14:28:11 UTC
I haven't used gcc, but if what you have does the compile and link, I would suspect trying



gcc file.c file2.c file3.c



Might work for several files.



If it doesn't, search the Web for gcc documentation. It's out there.



Hope that helps.
?
2009-08-30 06:00:39 UTC
Hi there again,

I thought I'd suggest this for everyone instead of emailing you.

One of the best compilers, ( FREE ), is the Open Watcom C/C++ compiler. It has a complete GUI, editor, image editor, extremely useful debugger and lots more. It is a full blooded compiler and is available for Windows, Linux, OS/2 and DOS. For the more ambitious, you can even download the source as it is an open project - however, be warned, the source is obviously huge!

You can get it at;



http://www.openwatcom.org/index.php/Main_Page



For the downloads, ( complete with installers );



http://openwatcom.mirrors.skynet.be/pub/ftp.openwatcom.org/



and choose the one you need.



Windows version is the;



open-watcom-c-win32-..>



and Linux is the;



open-watcom-c-linux-1.8



just to avoid any confusion.

Once downloaded simply click on the file and it will auto install for you.

This is my main compiler that I use for various projects and has full win32 API support etc.

Oh, don't forget the manuals at;



http://www.openwatcom.org/index.php/Manuals



Hope this is useful.



Regards.
jplatt39
2009-08-28 22:00:27 UTC
To compile two files named prog1.c and prog2.c into a single executable file called progname (or progname.exe in Windoze) with gcc type:



gcc -o progname prog1.c prog2.c



If you leave out the -o progname then of course gcc will use the default output file name which is a.out (or in Windoze a.exe).



Makefiles will generally do a two stage compilation. Thus:



gcc -C prog1.c -o prog1.o

gcc -C prog2.c -o prog2.o

gcc prog1.o prog2.o -o progname



In other words it compiles butt doesn't link the programs into object files, then links the object files into the executable.



It's worth reading about Makefiles even if you don't feel ready for them, so here is one tutorial:



http://makepp.sourceforge.net/1.19/makepp_tutorial.html



Yes it is for C++ files but the same rules apply to C files.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...