Code compiles in CLion but not in Command Prompt - c++

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!)

Related

clang 14.00 : _GLIBCXX11_DEPRECATED_SUGGEST("std::bind");

Installed clang 14.0 using GNU/Guix's clang-toolchain package.
I am getting depreciated feature usage error when
trying to compile a simple code:
//build with: clang++ main.cpp
//main.cpp
#include <iostream>
int main() {
std::cout << "hello world!" << std::endl;
}
The error message is:
[Guix profile dir]/include/c++/backward/binders.h:130:6: error: expected ';' after class
} _GLIBCXX11_DEPRECATED_SUGGEST("std::bind");
The error happens regardless of which STL header
the code includes. For example, it fails when <string>
is included with the same error message.
Any idea how to resolve the error?
Update 1:
This seems to be a configuration error. It goes away if the compilation is done inside a Guix container environment:
guix environment --container clang-toolchain -- clang++ main.cpp -o main
Update 2:
[This turned out to be WRONG. It is related to lack of multilib support in gcc-toolchain]
The error still persists when running the same code in a Guix environment with the --pure option. This means the
error isn't due to environment setting.
There is also an older bug report addressing the same issue: https://bugzilla.redhat.com/show_bug.cgi?id=1896076. The
error message in the bug can be reproduced when running
the test code using emscripten:
emcc -o main.html main.cpp
The problem is the bug was already addressed in Fedora 33?
I am currently on: Linux/fedora 5.17.4-200.fc35.x86_64 with
a Guix environment: guix describe --format=channels:
(list (channel
(name 'guix)
(url "https://git.savannah.gnu.org/git/guix.git")
(branch "master")
(commit
"59b91fdbcd7e1a88d1d54d392ce5d5fddd9d96d1")
(introduction
(make-channel-introduction
"9edb3f66fd807b096b48283debdcddccfea34bad"
(openpgp-fingerprint
"BBB0 2DDF 2CEA F6A8 0D1D E643 A2A0 6DF2 A33A 54FA"))))
(channel
(name 'flat)
(url "https://github.com/flatwhatson/guix-channel.git")
(branch "master")
(commit
"f43c67e47f1ca83d045f35c31772c5e4eb64b2a2")
(introduction
(make-channel-introduction
"33f86a4b48205c0dc19d7c036c85393f0766f806"
(openpgp-fingerprint
"736A C00E 1254 378B A982 7AF6 9DBE 8265 81B6 4490")))))
Update 3: It seems the latest clang-toolchain#14.0.0 breaks when installed alongside gcc-toolchain#11.2.0. That is, in the same Guix profile. Downgrading gcc-toolchain to version 10.3.0 resolved the error.
The issue can be reproduced with:
guix shell clang-toolchain#14.0.0 gcc-toolchain#11.2.0 --container -- clang++ main.cpp
Output:
In file included from main.cpp:1:
In file included from /gnu/store/../profile/include/c++/iostream:39:
In file included from /gnu/store/../profile/include/c++/ostream:38:
In file included from /gnu/store/../profile/include/c++/ios:42:
In file included from /gnu/store/../profile/include/c++/bits/ios_base.h:41:
In file included from /gnu/store/../profile/include/c++/bits/locale_classes.h:40:
In file included from /gnu/store/../profile/include/c++/string:48:
In file included from /gnu/store/../profile/include/c++/bits/stl_function.h:1407:
/gnu/store/../profile/include/c++/backward/binders.h:130:6: error: expected ';' after class
} _GLIBCXX11_DEPRECATED_SUGGEST("std::bind");
^
/gnu/store/../profile/include/c++/backward/binders.h:130:7: error: C++ requires a type specifier for all declarations
} _GLIBCXX11_DEPRECATED_SUGGEST("std::bind");
^
/gnu/store/../profile/include/c++/backward/binders.h:165:6: error: expected ';' after class
} _GLIBCXX11_DEPRECATED_SUGGEST("std::bind");
^
/gnu/store/../profile/include/c++/backward/binders.h:165:7: error: C++ requires a type specifier for all declarations
} _GLIBCXX11_DEPRECATED_SUGGEST("std::bind");
Downgrading gcc to gcc-toolchain#10.3:
guix shell clang-toolchain#14.0.0 gcc-toolchain#10.3.0 --container -- clang++ main.cpp
Output:
Expected file(a.out) is produced

C++ error: invalid use of ‘::’ for `std::cout`

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.

g++ Compiler Giving Strange Errors in Terminal

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

minGW (with g++ 4.7.1) not compiling simple C++11 code (under WinVista)

I did my homework and searched for an answer here and on the net. The simple code below is not compiling:
#include <thread>
#include <iostream>
void hello()
{
std::cout << "Hello from thread " << std::endl;
}
int main()
{
std::thread t1(hello);
t1.join();
return 0;
}
It's very simple code, but I'm getting the following errors:
Thread_Cpp11_002.cpp: In function 'int main()'
Thread_Cpp11_002.cpp:14:5: error: 'thread' is not a member of 'std'
Thread_Cpp11_002.cpp:14:17: error: expected ';' before 't1'
Thread_Cpp11_002.cpp:15:5: error: 't1' was not declared in this scope
And I've tried a lot of things, none of which have worked:
Declaring t1 using thread instead of std::thread
Compiling with g++ 4.8.0
Using the following flags, individually and together: -pthread, -std=gnu++11, -std=c++0x, -std=c++11...
So, I think this question it worth being posted here.
These compilation were done through the command line. After I have it working I will try with Code::Blocks 12.11, which worked fine for C++98.
Remember, I'm using Windows Vista.
This is a well-known issue with some builds of MinGW (just search for mingw thread in your favorite engine). If you need thread support, you have to use another library (such as boost or the native Windows API) or a different build.

C++ Hello World trouble on OSX10.8

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.