-4

I'm learning the #include command. I want to write a program which prints some datas (square miles of certain seas), so I created an header file sea.h, in which I defined (using #define) some dates.

Then I want to include this last file on a new file sea.c, but I have some problem. My program sea.c begins as follows

#include <stdio.h>
#include <sea.h>

int main(void)
{stuff}

when I compile, I get several errors, all related to the first one: next to

#include<sea.h>

it is written:

!sea.h: no such file or directory.

How can I fix this?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
Joe
  • 121
  • 2
  • 6
  • Don't post images of text, less links thereof or external links in general without providing the all relevant parts in the question **itself**. – too honest for this site Nov 10 '16 at 00:19
  • sorry I didn't know that, It wasn't did with bad intentions – Joe Nov 10 '16 at 00:20
  • I edited, please stop downvote, I am new to to this forum. – Joe Nov 10 '16 at 00:27
  • 3
    if you have a `sea.h` file in that same folder location, change `` to `"sea.h"`. the `<>` are for system libraries, not user created, which get surrounded by quotation marks. – chickity china chinese chicken Nov 10 '16 at 00:27
  • ..and don't mind the downvotes, it probably was due to the (broken) image links you had previously in the question before editing and putting code. You're question looks fine.. – chickity china chinese chicken Nov 10 '16 at 00:31
  • You can put the `.h` file anywhere you want as long as you pass its path on the command line when you compile. For `gcc` it's the `-I` option – yano Nov 10 '16 at 00:42
  • If you read the standard on the subject of header files, the `"header.h"` notation may (or may not) look in some extra places for header files before it looks in the same places as the `` notation. It often works out that the compiler will look in the current directory for `"header.h"` and not for ``. However, you can invariably control where the compiler looks, typically with a `-I/some/directory` option (or `-I.` for the current directory), and then `#include ` will work fine. – Jonathan Leffler Nov 10 '16 at 03:18
  • See [What is the difference between `#include ` and `#include "filename"`?](http://stackoverflow.com/questions/21593/) – Jonathan Leffler Nov 10 '16 at 03:33
  • @downshift: I don't mind the downvotes, ok, but for this now I can't post question for 3 days. I'm a user of MSE with 6k reputation, I know how hateful can be to see someone posting silly question which makes think that he doesn't want to make any effort. However this is ABSOLUTELY NOT my case! Can someone help me to give me the possibility to ask questions BEFORE than 3 days? Many thanks and sorry again for the initial bad formatting. – Joe Nov 11 '16 at 20:04
  • 1
    @Joe, I respect your request and your intentions, and I understand your frustration, unfortunately I have relatively no reputation on any SE sites and thus I can't offer any suggestions ... perhaps message someone with more experience and more rep on this site like Olaf or Jonathan Leffler from the comments, or better yet ask on one of the the Meta Stack sites.. – chickity china chinese chicken Nov 11 '16 at 20:14
  • @downshift hey man, many thanks! really – Joe Nov 12 '16 at 02:07
  • @Joe sure anytime! sorry I couldn't have helped more.. cheers – chickity china chinese chicken Nov 12 '16 at 02:18

1 Answers1

1

The error should be easily fixable by changing the header from <sea.h> to "sea.h". Here's a full, working example.

Create the file sea.c:

#include <stdio.h>
#include "sea.h"

int main(void)
{
    printf("%s", sea());
    return 0;
}

Then, in the same location as sea.c create sea.h, with something like this, sea.h:

char* sea(void)
{
    return ("hello, I'm sea() from \"sea.h\"");
}

Done. Then, compile as you did before, and you should not get any errors, at least like the one had, since we changed <sea.h> to "sea.h", and the build should produce your executable, which should output:

hello, I'm sea() from "sea.h"

  • 1
    Hmmm; it is not a good idea to put function bodies into header files, so your example is not good. You've also not explained how `#include ` and `#include "sea.h"` differ. And there are ways to make `#include ` work correctly. – Jonathan Leffler Nov 10 '16 at 03:15
  • Thanks @JonathanLeffler, please offer a good example in an answer, and show those ways to make `#include ` work correctly. thanks. – chickity china chinese chicken Nov 10 '16 at 03:42
  • For the difference and the how to make `#include ` work, see the x-ref'd question: [What is the difference between `#include ` and `#include "filename"`?](http://stackoverflow.com/questions/21593/). I am not going to repeat that. For the header, `sea.h` should contain `extern char *sea(void);` (with the `extern` being optional, but I prefer it, and maybe header guards, though they're not crucial in this example); a file `sea.c` should contain the implementation of the function; and another file `program.c` should contain the code currently in `sea.c`. – Jonathan Leffler Nov 10 '16 at 03:47
  • So for as simple as this example is, and the question only mentions a `` header, I still need to add another file to implement the function. I'd rather keep the question as close to the original question, or, if my example is poor, someone else can offer a proper example and I will happily delete my answer. Thank you. – chickity china chinese chicken Nov 10 '16 at 03:51
  • It's conventional to put the code described by `file.h` into a source file `file.c` (hence `sea.h` and `sea.c`). Clearly, that doesn't happen with major system headers like ``, but in the absence of a good reason to do otherwise, that's how the names should work. However, the C compiler doesn't care about such human-oriented conventions. You could include the code of the function in the header if the function is defined `static inline char *sea(void) { return "…"; }`. If the non-inline function is in the header, only one file can include the header; that makes the header pointless. – Jonathan Leffler Nov 10 '16 at 03:51
  • You could have used `#define SEA_STRING "this is the string from sea.h"` as the content of the header and then used that in the code. You chose to expect a function in the header; the question didn't indicate that was what it contained. – Jonathan Leffler Nov 10 '16 at 03:53
  • @downshift: many thanks, it works. However it seems so strange, because I was copying the program from the book of Kelley-Pohl! However thanks again! – Joe Nov 11 '16 at 07:45