0

I'm new to C.

I've split my sorting program into three .c files compare_functions.c, insertion_sort.c and main.c, which includes the two.

In order to get the executable, I try the followings: (only related files in the current folder)

gcc -c insertion_sort.c compare_functions.c main.c
gcc *.o -o main.out

In addition to two headers compare_functions.h, insertion_sort.h, finally there are nine files in the folder, after ls them:

compare_functions.c insertion_sort.c    main.c
compare_functions.h insertion_sort.h    main.o
compare_functions.o insertion_sort.o    main.out

Finally the ./main.out just work fine but, say, if I have a program much larger(ie. a hundred of .c file), finally there will be a mess with 100*3 files, is the way described above still suitable?

NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64
  • It's not really clear what you're asking. If you have a large program it could very well have hundreds of code files. Why is that a problem? – JJF Dec 21 '16 at 13:51
  • 1
    You use a [`make file`](http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/) – PM 77-1 Dec 21 '16 at 13:52
  • 1
    I marked this as a duplicate of a corresponding C++ question, but everything from the answers to the duplicate question applies to C as well. – Sergey Kalinichenko Dec 21 '16 at 13:54
  • This question is much simpler compared to the duplicate, and has much simpler and straightforward answers, too. – Michael Foukarakis Dec 21 '16 at 13:56
  • @MichaelFoukarakis I think once you get into 100+ files, as OP suggests, it's no longer simple. – Sergey Kalinichenko Dec 21 '16 at 13:57
  • gcc has -o option to specify output file name. Just compile files one by one. If there are too many files, use some build tool - GNU make, for example. – olegst Dec 21 '16 at 13:57
  • 1
    What you outline is a consequence of multiple files being needed to build your program. You might find that the files can be grouped into libraries which you compile in separate directories — keeping the source in separate directories and maybe the headers in an 'include' directory — which can reduce the clutter per directory. With static libraries, you can sometimes avoid keeping all the object files around after you've built the library. That doesn't seem to be an option for shared libraries. – Jonathan Leffler Dec 21 '16 at 13:59
  • 1
    @olegst I think there is a benefit in going for the build tool right away, before the project becomes too large to manage. The sooner one learns about make/cmake/automake/etc, the easier his life becomes. – Sergey Kalinichenko Dec 21 '16 at 13:59

1 Answers1

2

If you have a lot of source files to compile you might want to check tools like Automake or CMake. They will take care of compiling the right files in the right objects, AND they will only compile the files you edited.

Trucy
  • 146
  • 2
  • 14
  • 1
    I'd recommend good old make for the beginning. – olegst Dec 21 '16 at 13:59
  • I didn't know if I should recommand it as OP said that they wanted to compile hundreds of files, I tried to answer the question. Maybe I should have took in count that OP is new to C… – Trucy Dec 21 '16 at 14:03