0

I have a c++ header file socket_client.h together with a c++ source file socket_client.cc. In socket_client.h, I have code samples like this:

#include <limits.h>
#include <string>

ssize_t Send(const void *msg, const size_t msg_size/*send msg size*/);
...

In socket_client.cc, I have code snippet like this:

#include "somewhere/socket_client.h"
#include <sys/types.h>
...

When I compile this with gcc 4.8, the compiler will say "error" and complains that it can not find the definition of ssize_t; However, when I add #include <sys/types.h> in socket_client.h, it will compile success. There is someone else compile it successfully without add #include <sys/types.h> with gcc 4.4.

I'm totally confused about this. Is it necessary to add that line? What is the collect way to handle this?

ps:I compile with a tool called blade which is based on scons(and so do those with gcc 4.4).

user3162587
  • 233
  • 1
  • 6
  • 18
  • 2
    Well you *do* use `ssize_t` in the `socket_client.h` header file. And you include that header file *before* you include the `` file in `socket_client.cc`. Remember that symbols must be declared *before* they are used. – Some programmer dude Dec 13 '17 at 14:52
  • 1
    What system header are you including complaining about the absence of `ssize_t`? Some system headers require that `` is included (although I can't find one right now in the manages on MacOS). Things may still compile, e.g., because the declaration came from somewhere else in on version of the compiler/standard library. – Dietmar Kühl Dec 13 '17 at 14:54

2 Answers2

2

The answer is simple, have a look at your include order. You include socket_client.h before you include types.h. This causes your error. Simply change the order and everything should be fine.

xMutzelx
  • 566
  • 8
  • 22
  • Thanks. But why there is someone compile successfully with the same code using lower version gcc? – user3162587 Dec 13 '17 at 15:34
  • Because some other header did include `` or some other system header without you noticing that. – Basile Starynkevitch Dec 13 '17 at 16:12
  • @user3162587 This might be the case because some other header in 4.4 included and that this fact changed in 4.8. Maybe some include order has changed over the versions. There are many different reasons possible. – xMutzelx Dec 14 '17 at 08:25
  • Is it always a correct practice to include those system header files before self defined header files? Or should I include necessary system header files in self defined header file? – user3162587 Dec 14 '17 at 09:42
  • @user3162587 I think the overall order is personal, mine is: Corresponding hpp -> Headers from the same component -> Headers from other components -> System headers. For a little discusion check this out: https://stackoverflow.com/questions/2762568/c-c-include-file-order-best-practices – xMutzelx Dec 14 '17 at 11:06
0

GCC 4.8 is really old (and 4.4 is completely obsolete) since from 2013 and not really supported anymore. You should upgrade your GCC compiler, at least to GCC 6, and if possible to GCC 7 (and in spring 2018, that would be GCC 8).

(with efforts, you could compile and install GCC 7 from its source code; and you don't always need root access for that)

You need to define for what C++ standard you want to code. I strongly recommend coding in C++11 at least, and if possible C++14. In a few months (e.g. mid 2018), C++17 could be convenient. C++11 is really different of its predecessors (so it is not even worthwhile to code for an older standard, unless you are forced to).

If you code for C++11, you want to use at least std::size_t (instead of the old C-specific size_t) from <cstddef> or <cstdlib> etc... - and that standard header should be included before (or inside) yours, e.g. "somewhere/socket_client.h"; with a recent g++, you should compile with g++ -std=c++11 -Wall -Wextra -g (to get C++11, all warnings, and debug info). Of course, if you want C++14, use -std=c++14 ....

C++ is a very difficult programming language; if you are (like most of us, me included) still learning it, be sure to learn at least C++11 (or C++14) with some good C++ programming book.

Look also into some good C++ reference website (and even the appropriate C++ standard document).

BTW, you could find many libraries usable from C++ related to networking. Consider perhaps libcurl (client side) and libonion (server side) if you want to develop or use some Web API with HTTP, or POCO, or Qt, or Boost, or 0mq for general messaging (and there are many other libraries).

Most versions of GCC (even very old ones) support the -H and -M preprocessor options. You could use them (at least to understand what system headers get included).

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