Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

size_t can not be found by g++-4.1 or others on Ubuntu 8.1

This has happened before to me, but I can't remember how I fixed it.

I can't compile some programs here on a new Ubuntu install... Something is awry with my headers.

I have tried g++-4.1 and 4.3 to no avail.

g++ -g -frepo  -DIZ_LINUX -I/usr/include/linux -I/usr/include -I/include  -c qlisttest.cpp
/usr/include/libio.h:332: error: ‘size_t’ does not name a type
/usr/include/libio.h:336: error: ‘size_t’ was not declared in this scope
/usr/include/libio.h:364: error: ‘size_t’ has not been declared
/usr/include/libio.h:373: error: ‘size_t’ has not been declared
/usr/include/libio.h:493: error: ‘size_t’ does not name a type
/usr/include/stdio.h:294: error: ‘size_t’ has not been declared
...

the file...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
...



@ubuntu:~/work/zpk/src$ cat /usr/include/linux/types.h | grep size_t
typedef __kernel_size_t    size_t;
typedef __kernel_ssize_t   ssize_t;

types.h is definitely in the path, and is getting picked up. I verified by changing the file name and get an error its missing...

Does anyone have any ideas...? I would really appreciate the help...

like image 582
EdH Avatar asked Jul 10 '09 06:07

EdH


3 Answers

Start by removing -I/usr/include/linux and -I/usr/include. Adding system directories to include paths manually either has no effect, or breaks things. Also, remove -frepo for extra safety.

like image 88
Vladimir Prus Avatar answered Nov 15 '22 19:11

Vladimir Prus


Generally, you shouldn't be using C .h files for C++. While you may find an easy way to get away with it, and while a lot of this was allowed in previous versions of g++ and in other compilers, the C++ standard defines size_t to be in cstddef (see section 18.2/table 17). g++ has been only getting more strict.

Remove all the includes paths you've added to your command (they are redundant), and add to the top of your source code if not included:

#include <cstddef>
using namespace std;
like image 33
Samat Jain Avatar answered Nov 15 '22 19:11

Samat Jain


It's hard to say what the issue is without seeing your complete source. The best way to debug issues like this is to use g++'s "-E" parameter to produce pre-processor output, and then look at that to figure out what's going on in your includes. Here's what the g++ info page says about "-E":

-E Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output.

Also, why not just include sys/types.h at the top of the file?

Addendum:

On my system, I've created a short file called foo.cc that contains only:

#include <time.h>

And then I've run:

g++ -E /tmp/foo.cc > /tmp/foo.pp

Looking at this output in much detail is very important. For example, I learned that /usr/include/bits/types.h has a typedef for __time_t, and that /usr/include/types.h then uses that typedef to say "typedef __time_t time_t". But, there are interesting other macros surrounding that definiton. Pay special attention to things like the macro "__BEGIN_NAMESPACE_STD" in /usr/include/time.h, which on my system seems to be an empty define. But, I can imagine that some other systems may have a different value for this macro, forcing the definition of time_t into some other namespace.

Read the Cpp info page, section "9 Preprocessor Output" that defines the format of the lines of the file. Of particular note is the section on:

Source file name and line number information is conveyed by lines of the form

# LINENUM FILENAME FLAGS

And then goes on to describe "FLAGS" which are of interest for this level of debugging.

like image 42
slacy Avatar answered Nov 15 '22 18:11

slacy