0

I have four files containing C code.

Headers.h - (contain all necessary) headers

AddStudent.h - that file include Headers.h also introduce some function delegation

AddStudent.c - contains functions described in AddStudent.h

main.c - contains main()

The question is how to compile the code with cc ?

4
  • 1
    cc main.c -o main -I. BUT function implementations should be left in a C file, so AddStuden.h should actually be AddStudent.cpp since they are implementations of functions from AddStudent.h. In this case, you need to do: cc main.c AddStudent.c -o main -I. Commented Jan 6, 2012 at 18:49
  • 1
    homework should be tagged as homework Commented Jan 6, 2012 at 18:50
  • I assume you mean addstudent.c (as well as addstudent.h) Commented Jan 6, 2012 at 18:51
  • @user1074077, it wasn't, so I fixed it. Commented Jan 6, 2012 at 18:53

1 Answer 1

2

In your case, your probably just need:

cc main.c AddStudent.c

The right thing to do is make a makefile. Here's a (probably a bit naive) example:

myapp: main.o AddStudent.o
  cc -o myapp main.o AddStudent.o

main.o: main.c AddStudent.h Headers.h
  cc -c -o main.o main.c

AddStudent.o: AddStudent.c AddStudent.h Headers.h
  cc -c -o AddStudent.o AddStudent.c

The best place to learn about make is the GNU Make Manual.

Bonus note - if you're starting to learn C, you might want to check out clang. It gives way better error messages than gcc does, in addition to supporting C99 without special flags and being much faster at compiling.

Sign up to request clarification or add additional context in comments.

7 Comments

Can you recommend me what naming convention to use ?
I think it's fine now - your original post had a typo that made it look weird.
It's a little bit scary. Will be pain if I have > 20 .h/.c files.
@user1074077, makefiles support variables and template rules and so forth. Check out the manual for plenty of examples.
Just cc main.c AddStudent.c gives an error but with -std=c99 looks fine
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.