Question:
How do I compile and run a c program composed of multiple parts?
ryuzog
2010-12-28 22:40:50 UTC
What are the specific commands I need to issue in GCC to compile a program consisting of multiple parts?

The specific one I'm trying to run has 2 .c files and 2 .h files.

When I just run it the normal way I'd run a .c program, gcc program.c, many functions in the other .c program that it uses show up as undefined. I guess gcc doesn't automatically search the folder and "link" the files...
Four answers:
JoelKatz
2010-12-28 22:43:15 UTC
There are two ways:



1) Compile them together.

gcc file1.c file2.c -o myprogram



2) Compile them separately, then link them

gcc -c file1.c

gcc -c file2.c

gcc file1.o file2.o -o myprogram
severn
2016-10-27 03:20:26 UTC
i take advantage of Code::Blocks with Ubuntu merely about entirely for C/C++ initiatives. My merely grievance is that its source code reformatting is weak. i assume you could launch a compiler from nano, yet once you'll use a textual content fabric fabric editor extremely of an IDE, then emacs is significantly better useful and keeps to be the most excellent for a programmer's textual content fabric fabric editor. For an excellent source, evaluate taking off up Linux Programming (4th ed.) by potential of Matthew and Stones. It covers an excellent type of floor, from shell programming to programming applications or perchance gadget drivers in C. No examples in C++ (no longer interior the 0.33 version that I very own, besides) yet it truly is not important. Linux/Unix gadget calls aren't any more merchandise oriented, so as that the header information and the calling syntax is precisely the similar in C++ as in C.
prosanilin
2010-12-28 22:54:09 UTC
To compile '.c' file with gcc you need to write" $ gcc -Wall program_name.c -o program_name".Here the output file for the machine code is specified using the -o option.The option -Wall turns on all the most commonly-used compiler warnings---it is recommended that you always use this option!

To run the program, type the path name of the executable like this:

$ ./program_name
Nick T
2011-01-01 15:46:45 UTC
Use a make file!


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