C++ Hello World trouble on OSX10.8 - c++

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.

Related

Code compiles in CLion but not in Command Prompt

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

Importing cstring fails: include <cstring> error

I'am working on a group project with ROS and C++ on Ubuntu 16.04. Everything is working fine until two weeks ago I got this error
message out of the blue:
In file included from /usr/include/c++/5/cstring:42:0,
from /usr/include/boost/math/special_functions/detail/fp_traits.hpp:23,
from /usr/include/boost/math/special_functions/fpclassify.hpp:20,
from /usr/include/boost/math/special_functions/round.hpp:16,
from /opt/ros/kinetic/include/ros/time.h:58,
from /opt/ros/kinetic/include/ros/ros.h:38,
from /home/benjamin/Documents/bob_ws/src/tutorial/hello_world/src/hello_world.cpp:1:
/usr/include/string.h: In function ‘size_t strlen(const char*)’:
/usr/include/string.h:395:6: error: ‘cout’ was not declared in this scope
cout << *__s;
^
/usr/include/string.h:395:6: note: suggested alternative:
In file included from /opt/ros/kinetic/include/ros/time.h:54:0,
from /opt/ros/kinetic/include/ros/ros.h:38,
from /home/benjamin/Documents/bob_ws/src/tutorial/hello_world/src/hello_world.cpp:1:
/usr/include/c++/5/iostream:61:18: note: ‘std::cout’
extern ostream cout; /// Linked to standard output
^
In file included from /usr/include/features.h:367:0,
from /usr/include/stdlib.h:24,
from /opt/ros/kinetic/include/ros/platform.h:53,
from /opt/ros/kinetic/include/ros/time.h:53,
from /opt/ros/kinetic/include/ros/ros.h:38,
from /home/benjamin/Documents/bob_ws/src/tutorial/hello_world/src/hello_world.cpp:1:
/usr/include/string.h:396:6: error: expected primary-expression before ‘)’ token
__THROW __attribute_pure__ __nonnull ((1));
As a newbie to C++ and ROS my first thought was that I messed up by installing / removing some packages. So I tried reinstalling the whole ros framework. Didn't work, so I wrote a little programs
which (I think) shows what my problem is. I cannot include <cstring>:
#include <cstring>
int main(int argc, char** argv)
{
return 0;
}
When compiling that with g++ -Wall a.cpp -o atest I got a 2500 row error message.
Now I think there could be a little problem with cstring, but I have sadly no idea how to solve it.
My googling suggests that locate cstring | grep include might be of interest:
/usr/include/boost/compatibility/cpp_c_headers/cstring
/usr/include/boost/python/docstring_options.hpp
/usr/include/boost/test/utils/basic_cstring
/usr/include/boost/test/utils/basic_cstring/basic_cstring.hpp
/usr/include/boost/test/utils/basic_cstring/basic_cstring_fwd.hpp
/usr/include/boost/test/utils/basic_cstring/bcs_char_traits.hpp
/usr/include/boost/test/utils/basic_cstring/compare.hpp
/usr/include/boost/test/utils/basic_cstring/io.hpp
/usr/include/c++/4.8/cstring
/usr/include/c++/5/cstring
/usr/include/qt4/Qt/q3cstring.h
/usr/include/qt4/Qt3Support/q3cstring.h
Maybe it is a problem that there are two versions of C++ installed?
I'am pretty close to setting my system up
from scratch so you guys are my last hope.
Thanks to #molbdnilo and #Konrad Rudolph I figured that somehow my /usr/include/string.h (which is a C Header file) tried to import /usr/include/c++/5/iostream (which is a c++ lib).
After a vim diff with a working string.h I deleted the divergent lines wich was only the include statement for iostream.
As #digitalarbeiter recommended it is in general a bad idea to manualy change some code in this kind of files so I reinstalled libc6-dev with
sudo apt-get install libc6-dev --reinstall
and it still works.
I still have no idea how this could happen. The file was edited in april and the error occured in october. Anyway thanks to all the helpers!

C++ header file 'wchar.h' not found using g++ (MacOS)

Disclaimer - I am completely new to C++ and the way this language works regarding compiling / linking. Using MacOS Mojave.
For a school course we are obliged to use the g++ compiler to compile our c++ projects. G++ seems to be successfully installed; g++ -v results in the following output:
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-apple-darwin17.5.0/8.1.0/lto-wrapper
Target: x86_64-apple-darwin17.5.0
Configured with: ../gcc-8.1.0/configure --enable-languages=c++,fortran
Thread model: posix
gcc versie 8.1.0 (GCC)
I created my first "hello world" program:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
When I tried to compile this code (using 'g++ file.cpp), the following terminal output prompted:
In file included from /usr/local/include/c++/8.1.0/bits/postypes.h:40,
from /usr/local/include/c++/8.1.0/iosfwd:40,
from /usr/local/include/c++/8.1.0/ios:38,
from /usr/local/include/c++/8.1.0/ostream:38,
from /usr/local/include/c++/8.1.0/iostream:39,
from ex1.cpp:1:
/usr/local/include/c++/8.1.0/cwchar:44:10: fatale fout: wchar.h: No such file or directory
#include <wchar.h>
^~~~~~~~~
compilation ended.
The whcar.h file is indeed not present in the above folder. After some digging, I found the whcar file in the following folder: /Library/Developer/CommandLineTools/usr/include/c++/v1
Unfortunately I am a bit lost and don't really know what I am talking about, consindering everything is really new for me.
It is greatly appreciated if anyone can guide me in the right direction.
Kind regards,
Thijmen.
For me, reinstalling Xcode Command Line Tools and g++ solved the same issue:
xcode-select --install
brew reinstall gcc
The last line is assuming you have installed homebrew.
I just found out the easiest way of this mess is just download the missing header file to your compiler folder. It will fix everything.

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.

Running c++ file with raspberry pi

I was making a simple hello world c++ program. For some reason, it won't run after I compile it. Here's the program:
#include <iostream>
using namespace std;
int main() {
cout << "hello world";
}
I compiled using:
g++ -std=c++0x helloworld.cpp
No errors. However, when I tried running it using ./helloworld.cpp, I got this:
./helloworld.cpp: line 2: using: command not found
./helloworld.cpp: line 5: syntax error near unexpected token `('
./helloworld.cpp: line 5: `int main()'
Of course, I tried looking this up, and found a link that had someone asking almost the exact same question as mine. (C++ compiles but gives error when executed). They told me to remove the .cpp. However, I tried doing ./helloworld and I still got errors. It told me this:
bash: helloworld: No such file or directory
Also, I was in the directory with helloworld.cpp in it, so I don't think that was the problem. Any help would be appreciated. Thanks!
g++ -std=c++0x helloworld.cpp
should have left you with an a.out file that you can execute.
However, when I tried running it using ./helloworld.cpp, I got this:
...
You can't execute the helloworld.cpp source from the shell.
You probably should use
g++ -std=c++0x helloworld.cpp -o helloworld
# ^^^^^^^^^^^^^
to name the executable file other than a.out
You can call ./helloworld then to run your compiled program.
You can't execute a .cpp file. Find where the compiled program is and run that.
The .cpp file is the file you wrote. It's a text file, so you obviously can't "run" it. If you build a program you create an executable, which you can execute. This is a different file.