GCC errors

How to fix C++ ‘fatal error: stringstream: No such file or directory’

Problem:

Your C++ code contains a line like

#include <stringstream>

since you want to use std::stringstream, but your compiler gives you this error message:

main.cpp:2:10: fatal error: stringstream: No such file or directory
 #include <stringstream>
          ^~~~~~~~~~~~~~
compilation terminated.

Solution:

The header is called sstream, not stringstream! Use this #include directive instead:

#include <sstream>
Posted by Uli Köhler in C/C++, GCC errors

How to fix undeclared O_RDONLY/O_RDWR/O_WRONLY in C

Problem:

You are trying to compile C sourcecode that uses open(), but you see a compiler error message like this:

main.c: In function ‘main’:
main.c:3:14: warning: implicit declaration of function ‘open’ [-Wimplicit-function-declaration]
     int fd = open("my.txt", O_RDONLY);
              ^~~~
main.c:3:29: error: ‘O_RDONLY’ undeclared (first use in this function)
     int fd = open("my.txt", O_RDONLY);
                             ^~~~~~~~
main.c:3:29: note: each undeclared identifier is reported only once for each function it appears in

or

main.c: In function ‘main’:
main.c:3:14: warning: implicit declaration of function ‘open’ [-Wimplicit-function-declaration]
     int fd = open("my.txt", O_RDWR);
              ^~~~
main.c:3:29: error: ‘O_RDWR’ undeclared (first use in this function)
     int fd = open("my.txt", O_RDWR);
                             ^~~~~~
main.c:3:29: note: each undeclared identifier is reported only once for each function it appears in

or

main.c: In function ‘main’:
main.c:3:14: warning: implicit declaration of function ‘open’ [-Wimplicit-function-declaration]
     int fd = open("my.txt", O_WRONLY);
              ^~~~
main.c:3:29: error: ‘O_WRONLY’ undeclared (first use in this function)
     int fd = open("my.txt", O_WRONLY);
                             ^~~~~~~~
main.c:3:29: note: each undeclared identifier is reported only once for each function it appears in

Solution:

Add

#include <fcntl.h>

to the top of the source file where the error occured.

The POSIX header fcntl.h includes the definitions for O_RDONLY, O_RDWR and O_WRONLY amongst other POSIX-related definitions.

Also, this will include the definition for open() itself, which is missing as well – this is indicated by this line in the error message:

main.c:3:14: warning: implicit declaration of function ‘open’ [-Wimplicit-function-declaration]

See the OpenGroup reference on fcntl.h for more details on what else is defined in fcntl.h and what the meaning of all the individual flags is.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C warning ‘implicit declaration of function _exit’

Problem:

You have C code like

_exit(1);

but when you try to compile it you see a warning message like

main.c: In function ‘main’:
main.c:3:5: warning: implicit declaration of function ‘_exit’ [-Wimplicit-function-declaration]
     _exit(1);
     ^~~~~
main.c:3:5: warning: incompatible implicit declaration of built-in function ‘_exit’

Solution:

Add

#include <unistd.h>

at the top of the source file where the error occured.

Note that in case you use _Exit(...) (capital E) instead of _exit(...), you need to add

#include <stdlib.h>

instead.

Read the manpage for _exit in case you need further information on _exit(...).

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘RTLD_NEXT undeclared’

Problem:

You have C code like

dlsym(RTLD_NEXT, 'myfunc');

but when you try to compile it you see an error message like

main.c:3:11: error: ‘RTLD_NEXT’ undeclared (first use in this function)
     dlsym(RTLD_NEXT, 'myfunc');
           ^~~~~~~~~

Solution:

Add

#define _GNU_SOURCE
#include <dlfcn.h>

at the top of the source file where the error occured.

In order for RTLD_NEXT to be declared, #define _GNU_SOURCE must occur before the first #include statement!

Posted by Uli Köhler in C/C++, GCC errors

How to fix C warning ‘implicit declaration of function dlsym’

Problem:

You have C code like

dlsym(RTLD_NEXT, 'myfunc');

but when you try to compile it you see a warning message like

main.c: In function ‘main’:
main.c:3:5: warning: implicit declaration of function ‘dlsym’ [-Wimplicit-function-declaration]
     dlsym(RTLD_NEXT, 'myfunc');

Solution:

Add

#include <dlfcn.h>

at the top of the source file where the error occured. This will include both dlopen, dlsym and related definitions like RTLD_NEXT.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘unknown type name intptr_t’

Problem:

You have C code like

int v = 123;
intptr_t vptr = (intptr_t)&v;

but when you try to compile it you see an error message like

main.c: In function ‘main’:
main.c:5:1: error: unknown type name ‘intptr_t’; did you mean ‘__int128_t’?
 intptr_t vptr = (intptr_t)&v;
 ^~~~~~~~
 __int128_t
main.c:5:18: error: ‘intptr_t’ undeclared (first use in this function)
 intptr_t vptr = (intptr_t)&v;
                  ^~~~~~~~
main.c:5:18: note: each undeclared identifier is reported only once for each function it appears in

Solution:

Add

#include <stdint.h>

at the top of the source file where the error occured.

Posted by Uli Köhler in C/C++, GCC errors

How to fix va_list/va_arg related C error ‘expected expression before int’

Problem:

You have C code like

va_list ap;
int mode = va_arg(ap, int);

but when you try to compile it you see an error message like

main.c:5:23: error: expected expression before ‘int’
  int mode= va_arg(ap, int);
                       ^~~

Solution:

This error occurs because va_arg is undeclared. Add

#include <stdarg.h>

at the top of the source file where the error occured. This will include the definitions for va_list and the va_arg function.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘unknown type name va_list’

Problem:

You have C code like

va_list ap;
int mode = va_arg(ap, int);

but when you try to compile it you see a warning message like

main.c: In function ‘main’:
main.c:4:2: error: unknown type name ‘va_list’
  va_list ap;
  ^~~~~~~

Solution:

Add

#include <stdarg.h>

at the top of the source file where the error occured. This will include the definitions for va_list and the va_arg function.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C warning ‘implicit declaration of function va_arg’

Problem:

You have C code like

va_list ap;
int mode = va_arg(ap, int);

but when you try to compile it you see a warning message like

main.c:5:12: warning: implicit declaration of function ‘va_arg’ [-Wimplicit-function-declaration]
  int mode= va_arg(ap, int);

Solution:

Add

#include <stdarg.h>

at the top of the source file where the error occured. This will include the definitions for both va_list and va_arg.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘mode_t undeclared’

Problem:

You have C code like

mode_t mode;

but when you try to compile it you see an error message like

main.c: In function ‘main’:
main.c:4:2: error: unknown type name ‘mode_t’
  mode_t mode;
  ^~~~~~

Solution:

Add

#include <sys/stat.h>

at the top of the source file where the error occured. In case you want further information about what mode_t represents, I recommend reading this blogpost.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘EFAULT undeclared’

Problem:

You have C code like

errno = EFAULT;

but when you try to compile it you see an error message like

main.c:4:5: note: each undeclared identifier is reported only once for each function it appears in
main.c:4:13: error: ‘EFAULT’ undeclared (first use in this function)
     errno = EFAULT;

Solution:

Add

#include <errno.h>

at the top of the source file where the error occured. This will include both error codes like EFAULT and the global errno variable itself.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘errno undeclared’

Problem:

You have C code like

errno = EFAULT;

but when you try to compile it you see an error message like

main.c: In function ‘main’:
main.c:4:5: error: ‘errno’ undeclared (first use in this function)
     errno = EFAULT;
     ^~~~~
main.c:4:5: note: each undeclared identifier is reported only once for each function it appears in
main.c:4:13: error: ‘EFAULT’ undeclared (first use in this function)
     errno = EFAULT;

Solution:

Add

#include <errno.h>

at the top of the source file where the error occured. This will include both the errno variable and specific error codes like EFAULT.

Posted by Uli Köhler in C/C++, GCC errors

How to fix GCC error ‘implicit declaration of function printf’

Problem:

You have C code like

printf("test");

but when you try to compile it you see a warning like

main.c: In function ‘main’:
main.c:2:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
     printf("test");
     ^~~~~~
main.c:2:5: warning: incompatible implicit declaration of built-in function ‘printf’
main.c:2:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’

Solution:

Add

#include <stdio.h>

at the top of the source file where the warning occured.

Note that this warning message is just a warning and if you use printf correctly, your program will work even without #include <stdio.h>. However, incorrect usage of printf and similar functions will lead to hard-to-debug errors, so I recommend to add #include <stdio.h> in any case, even if it works without.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘false undeclared’

Problem:

You have C code like

bool mybool = false;

but when you try to compile it you see an error message like

main.c: In function ‘main’:
main.c:2:5: error: unknown type name ‘bool’; did you mean ‘_Bool’?
     bool mybool = false;
     ^~~~
     _Bool
main.c:2:19: error: ‘false’ undeclared (first use in this function)
     bool mybool = false;
                   ^~~~~
main.c:2:19: note: each undeclared identifier is reported only once for each function it appears in

Solution:

Add

#include <stdbool.h>

at the top of the source file where the error occured.

The reason for this error is that bool is not a standard type in C (like int or char) but only in stdbool.h. Also, true and false are declared in stdbool.h.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘true undeclared’

Problem:

You have C code like

bool mybool = true;

but when you try to compile it you see an error message like

main.c: In function ‘main’:
main.c:2:5: error: unknown type name ‘bool’; did you mean ‘_Bool’?
     bool mybool = true;
     ^~~~
     _Bool
main.c:2:19: error: ‘true’ undeclared (first use in this function)
     bool mybool = true;
                   ^~~~
main.c:2:19: note: each undeclared identifier is reported only once for each function it appears in

Solution:

Add

#include <stdbool.h>

at the top of the source file where the error occured.

The reason for this error is that bool is not a standard type in C (like int or char) but only in stdbool.h. Also, true and false are declared in stdbool.h.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘unknown type name bool’

Problem:

You have C code like

bool mybool;

but when you try to compile it you see an error message like

main.c: In function ‘main’:
main.c:3:5: error: unknown type name ‘bool’; did you mean ‘_Bool’?
     bool mybool;
     ^~~~
     _Bool

Solution:

Add

#include <stdbool.h>

at the top of the source file where the error occured.

The reason for this error is that bool is not a standard type in C (like int or char) but only in stdbool.h.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C error ‘NULL undeclared’

Problem:

You have C code like

char* ptr = NULL;

but when you try to compile it you see an error message like

main.c: In function ‘main’:
main.c:3:17: error: ‘NULL’ undeclared (first use in this function)
     void* ptr = NULL;
                 ^~~~
main.c:3:17: note: each undeclared identifier is reported only once for each function it appears in

Solution:

Add

#include <stddef.h>

at the top of the source file where the error occured.

The reason for this error is that NULL is not defined in C itself but only in stddef.h.

Posted by Uli Köhler in C/C++, GCC errors

How to fix C++ boost/array.hpp:118:61: error: expected primary-expression before ‘,’ token

In a legacy C++ project that is using Boost ProgramOptions. trying to compile it will yield this error message:

In file included from /usr/include/boost/lexical_cast/detail/converter_lexical.hpp:50:0,
                 from /usr/include/boost/lexical_cast/try_lexical_convert.hpp:42,
                 from /usr/include/boost/lexical_cast.hpp:32,
                 from /usr/include/boost/program_options/value_semantic.hpp:14,
                 from /usr/include/boost/program_options/options_description.hpp:13,
                 from /usr/include/boost/program_options.hpp:15,
                 from /home/uli/dev/myproject/datasplit.cpp:15:
/usr/include/boost/array.hpp: In member function ‘T& boost::array<T, N>::operator[](boost::array<T, N>::size_type)’:
/usr/include/boost/array.hpp:118:61: error: expected primary-expression before ‘,’ token
             return BOOST_ASSERT_MSG( i < N, "out of range" ), elems[i];

I didn’t find a satisfying way to fix this issue but it can be worked around fixing the issue in the source file:

First, open /usr/include/boost/array.hpp in your favourite editor as root (sudo!). I use nano.

Then, go to line 118 which reads:

return BOOST_ASSERT_MSG( i < N, "out of range" ), elems[i];

Replace that line by

BOOST_ASSERT_MSG( i < N, "out of range" );
return elems[i];

Also, 4 lines below what we just edited you’ll find another instance of

return BOOST_ASSERT_MSG( i < N, "out of range" ), elems[i];

Also replace that by

BOOST_ASSERT_MSG( i < N, "out of range" );
return elems[i];

Now. save the file and close your editor. Your code should compile now.

Posted by Uli Köhler in C/C++, GCC errors

How to fix GCC undefined reference to std::experimental::filesystem::…

Problem:

You want to compile an executable using GCC/G++ that uses functionality from std::experimental::filesystem but you get an error message like this during compilation / linking:

test.cpp:(.text+0x33): undefined reference to `std::experimental::filesystem::v1::file_size(std::experimental::filesystem::v1::__cxx11::path const&)'
/tmp/ccSXeCVb.o: In function `std::experimental::filesystem::v1::__cxx11::path::path<char [9], std::experimental::filesystem::v1::__cxx11::path>(char const (&) [9])':
test.cpp:(.text._ZNSt12experimental10filesystem2v17__cxx114pathC2IA9_cS3_EERKT_[_ZNSt12experimental10filesystem2v17__cxx114pathC5IA9_cS3_EERKT_]+0x73): undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status

Solution:

You need to link the stdc++fs library, i.e. append -lstdc++fs to your g++ command.

For example, a working command looks like this:

g++ -o myprogram main.cpp -lstdc++fs
Posted by Uli Köhler in C/C++, GCC errors

How to fix GCC error ‘the lambda has no capture-default’

When encountering a GCC error like this:

error: the lambda has no capture-default

fixing it is usually quite easy. Look for a Lambda function that captures some variable like this

[&myVar] (/* ... */) {/* ... */}

&myVar means “capture myVar by reference”.

In most cases you can just capture all local variables by using a capture default:

[&] (/* ... */) {/* ... */}

In rare cases this will have unintended side-effects as you now are capturing all variables by reference where you might want to capture some by copy – so be sure to check your code.

Note that this error is GCC version-dependent. For me using GCC 7.2 fixed the error.

Posted by Uli Köhler in C/C++, GCC errors