0

I am working in a linux environment. In my current directory, when I performed the ls command, this is what I got

enter image description here

In repo.c, I have this line of code

#include <battleship.h>

However when I try to compile and create the executable with this command(also link to library file battleship.c)

gcc repo.c -lbattleship.c

I get this error

"repo.c:10:24: error battleship.h: No such file or directory"

Does anyone know why the compiler cannot find the header file? I put it in the same directory as the main C file

11
  • 5
    Try to use #include "battleship.h", since battleship.h is a user defined library.
    – Shwetha
    Nov 6, 2014 at 7:06
  • Your user define headers shuld written as #include "battleship.h" and system define header are different notation which is like #include<stdio.h>. Nov 6, 2014 at 7:07
  • oh so only use <> for system libraries? Nov 6, 2014 at 7:08
  • try to compile like this gcc -o lbattleship lbattleship.c Nov 6, 2014 at 7:09
  • Isuru, not include repo.c at all? Nov 6, 2014 at 7:11

2 Answers 2

1
# include<> 

This variant is used to include system header files.

#include ""

This is used to include user created header files.

So try using

#include "battleship.h"
0

M guess is that the following command compiles the project:

gcc -o battleship repo.c battleship.c 

Theoretically the header file battleship.h should be included in battleship.c (or repo.c) like so (with quotes instead of brackets for local includes):

#include "battleship.h"
1
  • 1
    I strongly suggest compiling with all warnings and debug info, so gcc -Wall -Wextra -g repo.c battleship.c -o battleship. Later on, learn how to use make with your Makefile Nov 6, 2014 at 7:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.