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

How to fix GCC error: invalid use of incomplete type ‘class …’

Problem:

You are compiling a C/C++ program using GCC. You get an error message similar to this:

error: invalid use of incomplete type ‘class SomeType’

Solution:

There are multiple possible issues, but in general this error means that GCC can’t find the full declaration of the given class or struct.

The most common issue is that you are missing an #include clause. Find out in which header file the declaration resides, i.e. if the error message mentions class Map, look for something like

class Map {
   // ...
};

Usually the classes reside in header files that are similar to their name, e.g. MyClass might reside in a header file that is called MyClass.h, MyClass.hpp or MyClass.hxx, so be sure to look for those files first. Note that you might also be looking for a type from a library. Often the best approach is to google C++ <insert the missing type here> to find out where it might be located.

Another possible reason is that you have your #include clause after the line where the error occurs. If this is the case, ensure that all required types are included before they are used.

For other reasons, see StackOverflow, e.g. this post

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

How to resolve fatal error: bytecode stream generated with LTO version … instead of the expected …

Problem:

When compiling a program or library with GCC, you get an error message similar to this:

fatal error: bytecode stream generated with LTO version 5.1 instead of the expected 5.2

Solution:

This error basically means that you’ve got some binary build results (mostly object files) which are incompatible with other binary build results, but you’re trying to link them together. The LTO part just means that the incompatibility is due to you having link-time optimization enabled – but you don’t need to worry about that, you can just treat it like any other incompatibility.

In almost all cases, there is a very simple reason for the error: You have built your project with an older GCC version, then updated GCC and re-compiled (partially, as not all files have been changed) later. In this case, there is a simple solution: Just clean your build and re-build from scratch.

If you use make (or CMake), this is usually as simple as

make clean # Remove old, incompatible files
make # Rebuild

For other buildsystems, lookup how to clean your build accordingly – or just delete your build or dist directory if that doesn’t cause any unintended side effects.

In some very rare cases, an issue in the build system configuration causes the software to be built with two different compilers. If you think that might be the case (i.e. if the cleaning process does not help), I suggest trying to look at the verbose output of your build system — however, keep in mind that it’s more likely that you’re trying to link a stray object file that has been built with an older version of the compiler.

If you’re happy with just fixing the symptom while ignoring the possibility of hard-to-debug incompatibilities, you can just omit the -flto flag in the build system config. This hides the LTO incompatibility as it disables the link-time optimizer altogether, but even if no new error codes are shown, this approach is generally not recommended.

Posted by Uli Köhler in GCC errors