I am trying to compile the following code:
#include<stdio.h>
#include <algorithm.h>
int main() {
printf("hello, world!\n");
return 0;
}
But when I run emcc test.c -o test.html I get the following error:
fatal error: 'algorithm.h' file not found
When I remove the line that imports algorithm.h the code compiles perfectly.
Why is this happening? I was under the impression that algorithm.h was part of the standard library.
Edit:
I changed the name of the file from test.c to test.cpp, I updated the header names to <cstdio> and <algorithms>, and I also set -std=c++11 and it works now.
If this is C++ use
#include <cstdio>
in place of stdio.h and
#include <algorithm>
instead
In standard C++ there is no <algorithm.h> - there is only <algorithm>
Also in C++ the stdio header is both accessible from <cstdio> and <stdio.h> for compatibility.
Also since you are including algorithm the file extension should be .cc or .cpp and not .c or else emcc/gcc will treat it as a C source instead of a C++ one.
Related
I'm beginning to learn C++ programming, I'm using the visual studio editor.
This is the simple code I entered:
#include <stdio.h>
#include <iostream>
using namespace std;
int main(){
cout << "Hello, comply";
return 0;
}
I only added the "#include iostream" because my textbook says that is needed to let the program output to screen. I tried to compile and run in my command prompt and its giving me some error:
**C:\Users\edika\Desktop>gcc comply.c -o comply.exe
comply.c:2:22: fatal error: iostream: No such file or directory
#include <iostream>
^
compilation terminated.**
What am I doing wrong?
you're using a C compiler for C++. Additionally, your file extension should be *.cpp to indicate C++ code.
You're using gcc, you need g++.
Install G++, change your file name to comply.cpp, then run "g++ comply.cpp -o comply.exe"
I'm trying to compile a program that prints all available wifi access points including their BSSID.
The example of WlanGetAvailableNetworkList() function on MSDN website compiled just fine when removing the flag part with WLAN_AVAILABLE_NETWORK_CONNECTED.
To print BSSIDs as well I edited that code. I added the following functions and structs:
WlanGetNetworkBssList()
WlanScan()
PWLAN_BSS_LIST
PWLAN_CONNECTION_ATTRIBUTES
PDOT11_MAC_ADDRESS.
All of them except PDOT11_MAC_ADDRESS are marked as 'not declared in this scope'.
My compile command is:
g++ -o WlanAP2 WlanAP2.cpp -lwlanapi -lole32
(The same one worked for the MSDN example.)
My include order is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <wlanapi.h>
#include <objbase.h>
What am I missing?
Is there another library to be included?
Why do especially these commands not compile?
Thanks in advance!
I'm very new in Ubuntu and programming C++ on Ubuntu using Geany.
The problem I have here is that:
the classes i want to iclude to my project will receive an error,
I type,
#include <vector>
the error given here is,
fatal error: vector: No such file or directory
also I cannot use namespace std,
typing using namespace std returns the following error,
error: unknown type name 'using'
Here is the code:
#include <stdio.h> //no problem here
#include "stdlib.h" //no problem here
#include <vector> //this is a problem (lets say it returns error 1)
using namespace std; //this is a problem (lets say it returns error 2)
int main(int argc, char **argv)
{
return 0;
}
This sounds like you are using the wrong compiler to compile your C++ code. For example, by invoking gcc test.cpp the C++ file is actually compiled as C and you receive errors such as the one you posted - there is no vector header in C and there is also no using keyword.
If you are using gcc, the correct way to invoke the compiler to compile C++ is via the g++ symlink, i.e. g++ test.cpp
If you are using clang, the executable is called clang++ instead.
Both compilers support the -x parameter to manually change the language to C++, although in that case you also have to specify that the compiler needs to link your files with the C++ standard library. For example: gcc -x c++ test.cpp -lstdc++
In my makefile on this project, I am specifying 3 location for the compiler to find #includes.
INCLUDES=-I. -I/home/kelly/xerces/xerces-c-3.1.1/src -I/home/kelly/Utilities_New
When I compile the following sample code with the command line found in the comment:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <xercesc/util/XMLString.hpp>
using namespace std;
/*
g++ -I. -I/home/kelly/xerces/xerces-c-3.1.1/src -I/home/kelly/Utilities_New test.cpp
*/
int main(int argc, char* argv[])
{
cout << "this works" << endl;
}
In file included from /home/kelly/Utilities_New/string.h:5:0,
from /home/kelly/xerces/xerces-c-3.1.1/src/xercesc/framework/XMLBuffer.hpp:28,
from /home/kelly/xerces/xerces-c-3.1.1/src/xercesc/util/XMLString.hpp:26,
from test.cpp:7:
/home/kelly/Utilities_New/defs.h:26:21: fatal error: windows.h: No such file or directory
compilation terminated.
Clearly the compiler has decided that when it processes the Xerces #includes and finds string.h in the Utilities_New directory, it has found what it needs and stops there.
This other string.h was written by another programmer and I am attempting to use his library.
I thought that the standard locations were searched first. I'm a little lost here. I may be missing something super obvious to you all.
Also, wasn't there some rule about #include files that had <> vs. "" around them and where the compiler was supposed to look?
The standard locations are searched last.
#include "blah" is identical to #include <blah> unless blah is found in the same directory as the file that tries to include it. (And unless you use the rare and gcc-specific -iquote option).
For more information see here.
That's my code:
It works good on my mac.
But I'm not sure is that the problem of Gcc version or not.
they said the sstream and string header are wrong.
1779655.134485/Main.c:8:19: fatal error: sstream: No such file or directory
compilation terminated.
here is the hint
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
You are trying to include C++ header file in your C program.
Gcc is C compiler.
You need to rename Main.c to Main.cpp and use g++ compiler...