-4

Sorry. I think this question would be very easy to you guys.

I have two c files and one h file, I put those two .c files stack.c and main.c and one .h file stack.h inside a folder named "test" at Desktop.

So they are in C:\Users\user\Desktop\test

However when i try to test this code by writing

gcc -c stack.c sq_main.c -l stack.h

It continuously shows "unkown type name ..."

I think the header file is not included into those two .c files.

Actually I wrote the code

#include "stack.h"

Inside stack.c and main.c

Can anyone tell me how to include header file properly?

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
Dongjun LEe
  • 9
  • 1
  • 7
  • 2
    shouldn't it be `#include "stack.h"`? – undefined May 03 '15 at 16:20
  • If you could provide an [MCVE](http://stackoverflow.com/help/mcve) and the very first error or warning message produced by the compiler then it would be obvious what is wrong. Right now you are just expecting us to read your mind. – David Grayson May 03 '15 at 16:21
  • 1
    The syntax for includes is `#include "file" or ` you had `include "stack.h"`. I was wondering if that was a type-o on the question or an error in your code. – undefined May 03 '15 at 16:23
  • @DavidGrayson Sorry. to add some more information, i did some googling and found out that if those .c files cannot recognize the header file, then the cmd window will keep showing "stack.h error: unkown type name 'functino name'" sorry i was just a visual studio user and trying to do programming using gcc... – Dongjun LEe May 03 '15 at 16:24
  • @Nindaff: "*`... or `*" No, `#include`s never include libs, but always and ever files, at least if used correctly. – alk May 03 '15 at 16:26
  • @alk Yes that's a good point, I was trying to point out the difference in syntax for including a system headers. – undefined May 03 '15 at 16:30
  • @Nindaff thank you. i will re go through my code :D – Dongjun LEe May 03 '15 at 16:32
  • Please show full error output and some erroneous lines from stack.h – user3125367 May 03 '15 at 16:35
  • Did you consider installing Linux on your laptop. It might be simpler to use GCC on Linux (since it is its main native computer) than on Windows. – Basile Starynkevitch May 03 '15 at 16:45

1 Answers1

1

You are using GCC wrongly. I guess you are on Linux (or on something emulating it like MinGW ...)

If you insist on giving several commands in a terminal, you'll need to run

 gcc -Wall -Wextra -g -c stack.c
 gcc -Wall -Wextra -g -c sq_main.c

these two commands are building object files stack.o & sq_main.o (from stack.c & the #include-d stack.h, and sq_main.c & the #include-d stack.h, respectively). The options -Wall -Wextra are asking for all warnings and some extra warnings. The -g option asks for debugging information. The -c option asks for compiling only. Assuming that they are enough for your program, you need to link these object files to make an executable:

 gcc -g stack.o sq_main.o -o myprogram

You might need to add -Iinclude-directory options to the compiling commands (the first two), and you might need to add -Llibrary-directory and -llibrary-name to the linking command. Order of arguments to gcc matters a lot. You could also add -H to ask the compiler to show which files are included. And GCC has a lot of other options. Read the Invoking GCC chapter of its documentation.

The .o suffix might be .obj on most Windows systems. You might also need myprogram.exe instead of myprogram. I never used Windows so I cannot help more.

In practice, you should use GNU make and write some Makefile; this answer might inspire you.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547