using g++ to compile cpp file in macOS.
macOS v10.15.4
Apple clang version 11.0.3 (clang-1103.0.32.62)
hello.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "hello word" << endl;
return 0;
}
in terminal I run:
g++ hello.cpp
CPLUS_INCLUDE_PATH environment variable has an incorrect value.
Simple fix:
export CPLUS_INCLUDE_PATH=":/usr/local/include"
Similar problem happened for me on Catalina10.15.7 + gcc10.2 (homebrew), and CPLUS_INCLUDE_PATH method from Lin Weiye didn't work somehow.
Manually changing line 140 of ostream header from
include <locale>
to
include "locale"
did work. This will stop ostream from looking for locale executable via PATH, and force to look locale header in the same directory where ofstream header is.
Related
I installed mingw64 and also added the binaries folder to path and worked when I did g++ --version and gdb --version in cmd, but it doesn't work now. It compiles my simple code I wrote though through the terminal in VSC but I can't find anything in the "Run Build Task" menu in VSC.
VSC also gives me errors like iostream file not found, and cout is a undeclared identifier, and "Using directive refers to implicitly-defined namespace 'std'"
#include <iostream>
using namespace std;
int main()
{
cout << "hi";
return 0;
}
std::string file = "Cell.txt";
myfile.open (file);
makes a file in current program folder. i dont want the files mixed with the program that is writing them.
std::string file = "Cell\\Cell.txt";
does nothing
std::cout << file << '\n';
prints Cell\Cell.txt
i even tried
std::string file = "\\Cell\\Cell.txt";
did not expect this to work, but tried it anyway
std::string file = "\\\\Cell\\\\Cell.txt";
i have done it before, and can not fine anything on web to help
You say you're not using windows, and yet you create paths like this:
std::string file = "Cell\\Cell.txt";
This isn't a file called Cell.txt in a directory called Cell, but a file called Cell\Cell.txt because backslash path separators are a windows-ism, and under other operating systems they're part of the directory or file name. Use a forward slash instead: Cell/Cell.txt.
Better yet, use the new C++ filesystem libraries to build your paths in a platform-independent sort of manner, and avoid this issue entirely.
#include <experimental/filesystem>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
auto path = fs::path("Cell") / fs::path("Cell.txt");
std::cout << path.string() << std::endl;
}
This will output
Cell\Cell.txt
under windows and
Cell/Cell.txt
under linux, for example. You can also create directories using create_directory.
(note: this works out of the box on windows under vs2017 and probably 2015, but under g++ you'll need to include an extra library at compile time, like this: g++ -std=c++14 -O2 -Wall -pedantic main.cpp -lstdc++fs)
I am trying to compile a simple program but the MingW C++ compiler cannot find the path. I have two files one is C:\main.cpp the other one is C:\Include\test.h
#include <iostream>
#include "test.h"
using namespace std;
int main()
{
cout << "test" << endl;
getchar();
return 0;
}
I have modified the CPATH, CPLUS_INCLUDE_PATH enviroment vars to include the C:\Include path but it still will not compile with g++ c:\main.cpp -o c:\main.exe
Output from command line.
c:\main.cpp:2:18: fatal error: test.h: No such file or directory
compilation terminated.
Also I used this registry file. Still doesn't work.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment]
"LIBRARY_PATH"="C:\\Include"
"C_INCLUDE_PATH"="C:\\Include"
"CPLUS_INCLUDE_PATH"="C:\\Include"
There's not really enough information here, and storing source files in the root is suspect, but you might try:
g++ -I Include c:\main.cpp -o c:\main.exe
Assuming your cwd is C:\
This plus system restart was needed.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment]
"LIBRARY_PATH"="C:\\Include"
"C_INCLUDE_PATH"="C:\\Include"
"CPLUS_INCLUDE_PATH"="C:\\Include"
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = stoi(test);
std::cout << myint << '\n';
}
I tried this code on my computer which is running MinGW GCC 4.7.2. It gives me this error:
What am I doing wrong, I got this from cppreference. Its the exact same code. And its a different error from the one described here.
It seems your MinGW needs a patch: Enabling string conversion functions in MinGW
This patch enables the following list of C++11 functions and templates
in the std namespace:
stoi, stol, stoul, stoll, stof, stod, stold,
to_string, to_wstring
In above link, there is a .zip file, download it and
Copy wchar.h and stdio.h from the include directory in the zip file
to the following directory (overwrite): C:\mingw\include (replace
C:\mingw\ with the appropriate directory)
Copy os_defines.h to the following directory (overwrite):
C:\mingw\lib\gcc\mingw32\4.7.0\include\c++\mingw32\bits (replace
C:\mingw\ with the appropriate directory) (replace 4.7.0 with the
correct version number)
Another solution is to use MinGW-w64, which works correctly out of the box. This is a fork of MinGW that can produce both 32-bit and 64-bit builds.
I'm trying to compile the simplest program on MacOS 10.6 like:
$ g++ -o hello hello.cpp
the following source:
#include <iostream>
int main (int argc, char * const argv[]) {
std::cout << "Hello, World!\n";
return 0;
}
I'm getting the error:
hello.cpp:1:20: error: iostream: No such file or directory
hello.cpp: In function ‘int main(int, char* const*)’:
hello.cpp:4: error: ‘cout’ is not a member of ‘std’
So obviously I have to add the include path somewhere. My question is where can I find the include directories and how can add them globally (I don't want to provide the include path whenever I want to compile).
I just installed the XCode 3.1.4 and managed to compile it via Xcode, but not via command line. I found some header files in this directory:
/Xcode3.1.4/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers
and tried to add it to the HEADER_SEARCH_PATHS after reading this question, but no luck.
I'm developing on Linux and everything is working fine there, but I want to continue doing that on MacOS. Any help?
On my Mac, that include file is in /usr/include/c++/4.0.0/iostream . Are you sure
you have all the command-line development tools installed? They might not be by default;
I'm pretty sure I had to install it manually when I first set up my Mac. There should be a "developer tools" package somewhere on your OS X installation media.
Or, if you want to make sure you're getting the latest version, you can download it from:
http://developer.apple.com/technology/xcode.html
$ g++ -o example.bin example.cpp //to compile
$ ./example.bin //to run
It's code:
#include <iostream>
using namespace std;
int main () {
cout << "Hello, World!\n";
return 0;
}