Reputation: 233
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).
Upvotes: 0
Views: 1178
Reputation: 1
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).
Upvotes: 0
Reputation: 586
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.
Upvotes: 2