I recently installed xcode command line tools on my Mac. Its OS is version 10.9.5. To test out the g++ compiler I created a simple "hello world" c++ program, called it program.cpp and put it on my desktop:
#include <iostream>
using namespace std;
int main()
{
cout<<“Hello World”;
return 0;
}
Then I intended to create a compiled version of program.cpp, opened terminal, changed my directory to the desktop and typed the following command:
g++ -o program program.cpp
I received the following compilation errors:
program.cpp:7:11: error: non-ASCII characters are not allowed outside of
literals and identifiers
cout<<“Hello World”;
^
program.cpp:7:14: error: use of undeclared identifier 'Hello'
cout<<“Hello World”;
^
program.cpp:7:25: error: non-ASCII characters are not allowed outside of
literals and identifiers
cout<<“Hello World”;
^
3 errors generated.
Is this a problem with the command line tools or something else? Any help would be appreciated.
You aren't using double quote characters:
“Hello World”
is not the same as:
"Hello World"
I guess this is due to a copy-and-paste from PDF or something, as programs like Word like to change "" to “”.
Also if you are using the Xcode Command Line tools, you are actually using clang and not g++.
There might be an "U.S. International - PC" input method set on your mac (assuming you are on mac) and that has accented characters which result in '“' being inserted when you expected '"'.
enter link description here
Related
I am trying to a C++ library and need (want) to use nested namespaces in order to increase readability of the code. However, I am running into a problem when trying to compile my code in the Windows Command Prompt with g++ main.c.
The code below is an example of what I will have -- a nested namespace and then some functions or classes:
namespace gpc::warning {
void raiseError() {
std::cout << "Error...\n";
exit(1);
}
}
The code below is an example for my main.c file:
#include <iostream>
#include "Warning/raise.hpp"
int main() {
std::cout << "Hello, World!" << std::endl;
gpc::warning::raiseError();
return 0;
}
When I run this simple probram in CLion, it compiles and runs perfectly, however when I run the code in the Windows 10 Command Prompt, I get the following error telling me something about the namespaces:
In file included from main.cpp:2:0:
Warning/raise.hpp:10:14: error: expected '{' before '::' token
namespace gpc::warning {
^
Warning/raise.hpp:10:16: error: 'warning' in namespace '::' does not name a type
namespace gpc::warning {
^
main.cpp: In function 'int gpc::main()':
main.cpp:9:10: error: 'gpc::warning' has not been declared
gpc::warning::raiseError();
^
main.cpp: At global scope:
main.cpp:12:1: error: expected '}' at end of input
}
^
I was wondering what I am doing wrong and how I can fix this problem.
Thanks!
Try updating your g++ version to 6.1.0 or higher.
The code doesn't compiles on g++ v5.5.0 even with -std=gnu++17 flag. You can check that here. (Reason: Nested namespaces weren't supported by the compiler then.)
The code shall compile with compiler defaults (without any flags) on g++ v6.1.0 or higher. You can check this here.
You can check your compiler version by running: g++ --version on cmd.
Pro Tip: Locate your CLion's compiler and if it is g++ then add that one to path. (No need of wasting internet data on updating the old g++ compiler!)
I'm using vim on Mac OS X, and my vimrc contains these keymappings:
function! CPPSET()
set makeprg=if\ \[\ -f\ \"Makefile\"\ \];then\ make\ $*;else\ if\ \[\ -f\ \"makefile\"\ \];then\ make\ $*;else\ g++\ -std=gnu++0x\ -O2\ -g\ -Wall\ -Wextra\ -o\ %<\ %;fi;fi
set cindent
set nowrap
nnoremap <buffer> <F9> :w<cr>:!g++ -O2 % -o %< -std=c++14 -I ./<cr>:!./%<<cr>
nnoremap <buffer> <F8> :w<cr>:!g++ -Wall -Wextra -Wshadow -O2 % -o %< -std=c++11 -I ./<cr>
endfunction
I'm using these keymappings to both compile and run when I press F9. It works fine until today: I notice that the file won't compile and give the following error when I include these headers:
#include <stack>
#include <queue>
I notice that when I remove two these headers from my cpp file, it works fine. Moreover, when I try to compile that file (with these two headers included) with
g++ hello.cpp (instead of using F9 in Vim)
It works just fine. Please take a look! I've tried reinstalling xcode, but it didn't work.
In file included from queue.cpp:16:
./queue:1:1: error: source file is not valid UTF-8
<CF><FA><ED><FE><U+0007><U+0000><U+0000><U+0001><U+0003><U+0000>
<U+0000><80><U+0002><U+0000><U+0000><U+0000><U+0010><U+0000><U+0000>
<U+0000>p<U+0006><U+0000><U+0000><85>...
./queue:1:2: error: source file is not valid UTF-8
<CF><FA><ED><FE><U+0007><U+0000><U+0000><U+0001><U+0003><U+0000>
<U+0000><80><U+0002><U+0000><U+0000><U+0000><U+0010><U+0000><U+0000>
<U+0000>p<U+0006><U+0000><U+0000><85>...
./queue:1:5: error: expected unqualified-id
<CF><FA><ED><FE><U+0007><U+0000><U+0000><U+0001><U+0003><U+0000>
<U+0000><80><U+0002><U+0000><U+0000><U+0000><U+0010><U+0000><U+0000>
<U+0000>p<U+0006><U+0000><U+0000><85>...
^
./queue:1:6: warning: null character ignored [-Wnull-character]
<CF><FA><ED><FE><U+0007><U+0000><U+0000><U+0001><U+0003><U+0000>
<U+0000><80><U+0002><U+0000><U+0000><U+0000><U+0010><U+0000><U+0000>
<U+0000>p<U+0006><U+0000><U+0000><85>...
^
./queue:1:7: warning: null character ignored [-Wnull-character]
<CF><FA><ED><FE><U+0007><U+0000><U+0000><U+0001><U+0003><U+0000>
<U+0000><80><U+0002><U+0000><U+0000><U+0000><U+0010><U+0000><U+0000>
<U+0000>p<U+0006><U+0000><U+0000><85>...
...continue
Edit:
This is what Syntastic identifies: here
When I compile with g++ simply, it works seamlessly: here
Comments out #include , #include and Syntastic is satisfied:here
You have (according to your pictures) a binary file (probably an executable) named queue in your working directory. For some weird reason you need to discover, it gets included with #include <queue> (look into the actual program arguments passed to g++ by your editor or IDE). Remove that file.
(as a rule of thumb, avoid naming your programs with the same name as usual things on your computer; having an executable named queue is bad taste for a C++ developer)
You might want to pass -H to g++ to understand what files are included.
Then take several hours or days to read the documentation on Invoking GCC and learn to use some build automation tool, such as GNU make or ninja. For make, read documentation of make then write your own Makefile (caveat: tab characters are significant). For ninja see this. You need to understand how g++ should be invoked.
At last, read the documentation of your source code editor (or IDE) and configure it for your needs and tastes. You might want it to run some make or ninja command at a single function keypress. How to do that is explained in the documentation. For vim, see this.
The important thing to understand is that IDEs are just source code editors capable of running some external command to build your software. You can configure that command. C++ compilers are -practically speaking- always command line programs (usually GCC or Clang), and you need to learn how to invoke them (you generally should prefer to use a build automation tool to run your compilation commands).
This is probably the weirdest thing I have seen in the last few years.
I have a project that builds perfectly well in two completely different machines (openSUSE Tumbleweed and ubuntu 14.04).
I started with a new machine using kubuntu 16.04 and this error started happening:
$ g++ -std=c++14 cout_qualif.cpp -lpng -o cout_qualif
In file included from cout_qualif.cpp:1:0:
debug_utils.h:19:19: error: invalid use of ‘::’
# define msg std::cout
Clang also points an error, but with a quite different message:
$ clang -std=c++14 cout_qualif.cpp -lpng -o cout_qualif
In file included from cout_qualif.cpp:3:
In file included from /usr/include/png++/png.hpp:34:
In file included from /usr/include/png.h:317:
/usr/include/zlib.h:94:19: error: non-friend class member 'cout' cannot have a qualified name
z_const char *msg; /* last error message, NULL if no error */
^~~
./debug_utils.h:19:19: note: expanded from macro 'msg'
# define msg std::cout
~~~~~^
1 error generated.
The simplest test code I've came across is:
#include <iostream>
#include "debug_utils.h"
#include <png++/png.hpp>
int main()
{
msg << "Start" << std::endl;
png::image< png::rgb_pixel > image("input.png");
image.write("output.png");
msg << "Finish" << std::endl;
return 0;
}
And "debug_utils.h":
#ifndef DEBUG_UTILS_H
#define DEBUG_UTILS_H
#include <iostream>
# define msg std::cout
#endif // DEBUG_UTILS_H
It turns out that "png.h" includes "zlib.h" and there is defined a struct:
typedef struct z_stream_s {
// ...
z_const char *msg; /* last error message, NULL if no error */
This msg member is what is triggering the error. If I move my #include "debug_utils.h" one line below, after #include <png++/png.hpp>, everything seems to work fine.
Now finally the question:
Why this machine cannot compile my code while two other can?
Additional info:
Kubuntu 16.04:
$ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
$ clang --version
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
openSUSE Tumbleweed:
g++ is 7.1.1
Ubuntu 14.04:
Exact version not available at hand but I believe it is 4.9.x
To recap the cause of the problem
# define msg std::cout
in debug_utils.h substitutes std::cout for any instances of msg throughout the code after debug_utils.h is included. As msg is a common short identifier, particularly for a message buffer, an unintended substitution has always been a risk lurking in the code. The solution to this is obvious: Don't do that. Use a longer, less likely to be duplicated substitution or don't do it at all and replace the macro. Without seeing your usecase I'd probably replace the macro with a function that returns the correct stream and the compiler can easily inline.
The confusion and the question stems from why an error from the macro substitution only arises from a simple piece of test code when compiled on only one of three candidate PCs.
The answer is differences in the toolchain and support libraries. For one reason or another, on only one of these PCs a third-party header that uses the msg identifier is included by a third-party header included by the test program. The other two follow different inclusion paths to build the same program and avoid tripping over the unwanted substitution.
I'm a bit of a newbie to C++, but I have some programming experience. I made a basic program, following a guide I found on t'internet. It compiled with g++ easily, but when I ran it it threw these errors:
./FP.cpp: line 1: //: Is a directory
./FP.cpp: line 3: using: command not found
./FP.cpp: line 5: syntax error near unexpected token ('
./FP.cpp: line 5:int main ()'
I'm using Geany on a Raspberry Pi (but using a command line to run the program as the Geany interpreter doesn't work). Here's the program:
// First program in c++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
Any ideas?
Cheers!
It looks like you're trying to execute the source file, i.e. run it as a shell script.
To compile your program you would do something like this:
$ g++ -Wall FP.cpp -o FP
This produces an executable named FP. So you should now see both the original program, FP.cpp, and the executable, FP, in your current directory.
To run the executable (compiled program):
$ ./FP
Calling ./FP.cpp tries to execute the source code, which is not what you want. You need to compile it via g++, and then run the executable (usually ./FP)
My profile is updated to point to what i think is the right location:
PATH=${PATH}:/Applications/Xcode.app/Contents/Developer/usr/bin:${PATH}
Compilers are found
[11:39:32] ~: $ which g++
/Applications/Xcode.app/Contents/Developer/usr/bin//g++
[11:39:35] ~: $ which gcc
/Applications/Xcode.app/Contents/Developer/usr/bin//gcc
Hello world looks as follows
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
Fails to execute both from command line and TextMate with
[11:40:15] Desktop: $ g++ untitled.cpp
untitled.cpp:1:20: error: iostream: No such file or directory
untitled.cpp: In function ‘int main()’:
untitled.cpp:6: error: ‘cout’ was not declared in this scope
What else should i have in my path and am missing please?
The answer is, as always, obvious
Install Xcode Command Line Tools
Check your /usr/bin and find there:
Try again .. Everything works automagically
On a side note, it is odd that upgrade from OSX10.7 to OSX10.8 removed these links
This answer is for GCC, but other non-vcc compilers may be the same. When I used GCC iostream was non existent, however iostream.h was, I suggest you try iostream.h and if you run into this problem again with STL headers add the .h.