Include paths not found while compiling with g++ on MacOS - c++

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;
}

Related

Add SFML (third party library) to C++ project on Xcode

I'm learning how to build a simple UI in C++ on my Mac (OS 11.6) using Xcode.
As first step I'm compiling the "Hello world" program, my problem is that the build on Xcode fails but write my own command from terminal, instead, works.
This is the program, I'm using SFML :
#include <iostream>
#include "SFML/Graphics.hpp"
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
I have no error here but when launching Run from Xcode this is the output, in Graphics.hpp file :
#include <SFML/Window.hpp>. //'SFML/Window.hpp' file not found
#include <SFML/Graphics/BlendMode.hpp>
#include <SFML/Graphics/CircleShape.hpp
//other header files
This is how the project is structured ("TestGui" is the project name) :
-TestGui.xcodeproj
-TestGui(folder)
--SFML(directory with all headers file available
-- main.cpp
SFML source code here
So I tried to compile it with my own hands from terminal with :
g++ main.cpp -I ./SFML -o main
and
clang++ main.cpp -I ./SFML -o main
In both cases it compiled, also run worked.
Since the error is linked to a file not found I tried to tell it where libraries are located, so in Xcode from Product->Scheme->Edit Scheme->Run->Arguments->Arguments passed on launch : added -I ./SFML. But the error is still alive.
Added SFML folder to targets from Xcode, didn't copy-pasted but maybe I did it wrong, this is my first time.
EDIT : SFML folder:
--SFML
--- many .hpp files
--- 5 folders (Audio, Graphic, Network, System and Window)
I tried to add also this argument : -L ./SFML but nothing.

Cannot compile Gurobi examples in version 7.5.1

I just updated Gurobi to version 7.5.1 on Linux (Ubuntu)--this is the newest version available.
Problem Any time I try to compile any code that uses Gurobi--for example, the examples included in /opt/gurobi751/linux64/examples, I just get a string of undefined reference errors (e.g. undefined reference to GRBModel::set(...)). I would consider the problem to be solved if I can go to the directory /opt/gurobi751/linux64/examples/build and run the command make run_diet_c++ and have it compile and run.
Attempted Fixes
(1) I have set $GUROBI_HOME in my .bashrc file--it points to the correct directory. $LD_LIBRARY_PATH and $PATH have been updated as well. They all point to the correct directories.
(2) I have a valid Gurobi license. If I write a .lp file and run it like gurobi_cl model.lp, it runs correctly. Running gurobi_cl --version gives the expected output (i.e. version 7.5.1).
(3) If I try to compile the C version (with make run_diet_c) everything works as expected.
More Information
I created the following test file in my home directory:
#include <stdio.h>
#include "gurobi_c++.h"
int main(int argc, char **argv)
{
GRBVar x;
std::cout << "Hello, world!" << std::endl;
return 0;
}
I then compile using g++ with the following command:
g++ -Wall test.cpp -o executable -I/opt/gurobi751/linux64/include -L/opt/gurobi751/linux64/lib -lgurobi_c++ -lgurobi75
This compiles and runs without complaining. However, I tried this example:
#include <stdio.h>
#include "gurobi_c++.h"
int main(int argc, char **argv)
{
GRBEnv* env = 0;
try {
env = new GRBEnv();
GRBModel model = GRBModel(*env);
model.addVar(0, 1.0, 1.0, GRB_CONTINUOUS, "TheVar");
model.update();
model.optimize();
} catch (...) {
std::cout << "Exception during optimization" << std::endl;
}
delete env;
return 0;
}
and compiled it with the same command, and it failed. So it seems like the include statement is working fine, but somehow it's not linking to the library correctly?
Please let me know if more information is needed. Also, if it's not clear, I don't know very much about the compilation and linking process, which is probably hindering me here.
You need to compile the libgurobi_c++ for your g++ version.
First, go to the folder
cd /opt/gurobi751/linux64/src/build/
make
Now, you need copy the compiled file to lib folder:
cp libgurobi_c++.a ../../lib/
You will compile and run.

Linker error when linking C++ code with USB driver (ftdi)

I am on a Mac OSX and I am trying to start using the C ftdi drivers I installed using brew
brew install libftdi
That installed the library in the directory /usr/local/Cellar/libftdi/1.2 And I have the following C++ code
#include <iostream>
#include <ftdi.h>
using namespace std;
int main() {
struct ftdi_context ftdi;
ftdi_init(&ftdi);
cout << "Hello World" << endl;
return 0;
}
How should I compile this to make it work? I am including the include/ directory so the compiler can find the header file but I do not know which object file should be linked. I am trying to using the following command
g++ -I /usr/local/Cellar/libftdi/1.2/include/libftdi1/ /usr/local/Cellar/libftdi/1.2/lib/libftdi1.a test.cpp
But I am getting bad linker errors even though I have included the .a file. The lib/ directory has the following contents
cmake
libftdi1.2.2.0.dylib
libftdi1.2.dylib -> libftdi1.2.2.0.dylib
libftdi1.a
libftdi1.dylib -> libftdi1.2.dylib
pkgconfig
Is there something else I should try and link it to? Even if you are not aware of this particular driver is there something in general that I should be doing for drivers like this that I have not done?
Thanks for the help!

Reading From OGR error in c++

I'm trying to compile the following code using the gdal libraries in Centos 7:
The name of the file is rungdal.cpp
#include "/usr/include/gdal/ogrsf_frmts.h"
int main(){
// Register all format drivers
GDALAllRegister();
}
I run the program using: g++ rungdal.cpp -o rungdal, and I have the following message:
error: ‘GDALAllRegister’ was not declared in this scope
I also include the whole path from the header file, if I don't use it the program doesn't work, maybe it's something related.
What can I do for executing the program?
Thanks for your help!
I fixed the code with your suggestions:
#include <gdal/gdal.h>
int main(){
// Register all format drivers
GDALAllRegister();
}
In addition, I have to add the line -lgdal for the compilation and it works.
g++ rungdal.cpp -o rungdal -lgdal
Thanks for your help!
Alvaro

MingW environment paths

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"