Here is a simple program
#include <stdio.h>
int main(){
printf("Hello World\n");
return 0;
}
Compiling this program with Clang gives the error below.
$ clang test.cpp
test.cpp:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^
1 error generated.
The program above is a shortened version from a large project. I can neither change the program, or change the compiler to use, namely clang. So it seems to me the only options is to change environment variables so to alter the flags used by Clang. Which flag should I use?
Related
Recently, I wanted to use from_chars from c++17.
Looked at http://en.cppreference.com/w/cpp/utility/from_chars and found that code on this page:
#include <iostream>
#include <charconv>
#include <array>
int main()
{
std::array<char, 10> str{"42"};
int result;
std::from_chars(str.data(), str.data()+str.size(), result);
std::cout << result;
}
cannot be compiled by any of compilers. I tried on page http://en.cppreference.com/w/cpp/utility/from_chars and on godbolt with different compilers but all of them returned the same error:
<source>:2:10: fatal error: 'charconv' file not found
#include <charconv>
^~~~~~~~~~
1 error generated.
Compiler returned: 1
Can anybody help me with this, please?
(I tried clang 6.0, gcc 7.3 and msvc 19 but all of them returned error that 'charconv' not found)
According to GCC's libstdc++ status page, this header is only supported starting from GCC-8.1. You could either avoid using this header by detecting it:
#if __has_include(<charconv>)
#include <charconv>
#else
/* implement some fallback */
#endif
Or just update your compiler. This godbolt example confirms that the header is present in GCC-8.1.
If you use Clang, remember that Clang uses GCC's stdlibc++ by default. You will either need to update your GCC or switch to Clang's libc++ by:
clang++ -std=c++17 -stdlib=libc++ main.cpp
update gcc version to 8.3, seems like an older gcc version causing problems
#include <iostream>
using namespace std;
int main(){
std::cout << "Hello World\n";
return 0;
}
command 1 (works)
clang hello.cc -o hello -lc++
command 2 (don't works)
/path/to/custom/clang hello.cc -o hello -lc++
main.cc:2:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Why I can't compile with command 2 ?
It looks like you're trying to compile C++ with a C compiler. Try running clang++ instead.
clang++ hello.cc -o hello
Without running clang as a C++ compiler it won't have the C++ standard library headers available for you to include. Using clang++ the C++ standard library headers are available and the C++ standard library is linked for you automatically.
That is a known Ubuntu issue. Their clang just isn't set up right. I complained about it here -- and this remained unfixed for years.
But the good news is that it now works with the most recent 16.10 release.
Edit: Based on your updated question I would say that "custom clang" does not know about its include files.
I'm getting this error
clang: error: linker command failed with exit code 1 (use -v to see invocation)
while i'm compiling simple cpp file from terminal
gcc hello.cpp
here's the content of hello.cpp file:
#include <iostream>
using namespace std;
// main() is where program execution begins.
int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
I think it might conflict with XCode compiler?
gcc hello.cpp should be g++ hello.cpp
gcc is for compiling and linking C code, while g++ is used for C++ code as you have it.
I think it might conflict with XCode compiler?
No. The point is that the gcc command is also able to compile C++ code detected from the .cpp file extension, though the libstdc++.a won't be linked automatically.
I get the error
"/usr/include/c++/5/cstddef:51:11: error: ‘::max_align_t’ has not been declared
using ::max_align_t;
^"
So I should update the libraries because I find this solution:
"A workaround until libraries get updated is to include <cstddef> or <stddef.h> before any headers from that library."
I wrote some command on the Ubuntu terminal such as:
bash $ sudo apt-get install apt-file
bash $ sudo apt-file update
bash $ apt-file search stddef.h
Then still the error exist.
Thank you
In the .cpp file where this compile error occurs you need to add
#include <cstddef>
before any of the other headers, e.g.
main.cpp (broken)
#include <cstdio>
int main()
{
using ::max_align_t;
puts("Hello World");
return 0;
}
Try to compile that:
$ g++ -std=c++11 -o test main.cpp
main.cpp: In function ‘int main()’:
main.cpp:5:10: error: ‘::max_align_t’ has not been declared
using ::max_align_t;
^
Then fix it:
main.cpp (fixed)
#include <cstddef>
#include <cstdio>
int main()
{
using ::max_align_t;
puts("Hello World");
return 0;
}
Compile and run it:
$ g++ -std=c++11 -o test main.cpp
$ ./test
Hello World
I compiled some code with GNU C++ 4.9 on CentOS, and the issue was not solved by ensuring top position #include (or by the older header name stddef.h).
Weird enough, I searched all header files of the compiler libraries for the global definition of max_aling_t as declared in the offending using declaration... and found none! Could it be in some 'internal compiled header?
So I simply commented-out the "using ::max_align_t;" line in the standard header (not proud of doing this indeed) and it solved the problem... and code is running...
if anyone can explain what is the meaning/impact of this max_align_t ?
I also commented-out the using ::max_align_t; line in /usr/include/c++/4.9/cstddef, while, code is running, but I don't know if there are any consequences by doing this...
I got some quite strange errors compiling code under gcc. It tells me that std::function does not exist.
I can recreate the error with the following code:
#include <functional>
#include <stdio.h>
void test(){ printf ("test"); }
int main() {
std::function<void()> f;
f = test;
f();
}
If I run gcc (from cygwin): (my error message was German, so i translated it. It may be sound different on a English gcc)
$ gcc test.cpp
test.cpp: in function "int main():
test.cpp:7:3: Error: "function" is not an element of "std"«
test.cpp:7:25: Error: "f" was not defined in this scope
With MSVC it compiled successfully.
Please tell me what I am doing wrong in my code.
Johannes
Compile it as:
g++ test.cpp -std=c++0x
-std=c++0x is needed because you're using C++11 features, otherwise g++ test.cpp is enough.
Make sure you've latest version of GCC. You can check the version as:
g++ --version
You need to compile in C++ mode, and in C++11 mode. So you need g++ and the -std flag set to c++0x.
g++ test.cpp -std=c++0x
You can also use -std=c++11 from gcc 4.7 onwards.