How to query the default include paths of clang++? - c++

How can I query the default include path of clang/clang++? I am trying to use a custom built clang compiler (the one that supports OpenMP), but it doesn't seem to find the STL libraries:
/usr/local/bin/clang++ hello.cpp
hello.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
By using an IDE, back-tracking the #include iostream, and finally using the -isystem option I got the simple helloworld application to compile in OSX 10.9:
/usr/local/bin/clang++ -isystem /Library/Developer/CommandLineTools/usr/lib/c++/v1 hello.cpp
Thanks for your help!

You are looking for option -v. Compiling with clang++ -c file.cc -v will print among other things:
#include "..." search starts here:
#include <...> search starts here:
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9
etc.

If you run
clang++ -### hello.cpp
It will display the commands used to compile that particular file, including the default include paths, library search paths, targets etc.

Related

G++ seems to be ignoring #ifdef for an #include

Environment
Ubuntu 16.04
G++ 5.3.1
I have a header file with the following intended to include a different .h file depending on platform:
#ifdef _WIN32
#include "curses.h"
#else
#include <ncurses.h>
#endif
This works fine in windows but in Ubuntu I get errors about the curses.h file:
In file included from /usr/include/unctrl.h:54:0,
from /usr/include/curses.h:1694,
from headers/command_window.h:8,
from command_window.cpp:1:
headers/curses.h:900:19: error: macro "clear" passed 1 arguments, but takes just 0
int clear(void);
This when compiling with:
g++ -g -lncurses -std=c++11 -Iheaders -c -o command_window.o command_window.cpp
Why is headers/curses.h, which is the windows specific file for PDCurses being involved here at all?
/usr/include/unctrl.h contains this line:
#include <curses.h>
And since you've told the compiler to look in your headers/ folder for header files with the -Iheaders flag , the compiler picks up curses.h in that folder.
So you need to drop the -Iheaders flag (and e.g. use #include "headers/header_name.h") or you need to rename your headers/curses.h to not collide with /usr/include/curses.h
In your version of g++, the -I option is not the correct way to add application-specific header files (those that shouldn't be found by #include in system headers) to the search path (this change surprised me as well).
Instead, you should use -iquote headers.
See this answer: How to tell g++ compiler where to search for include files? and this official documentation

Installed freetype through homebrew, can't seem to include headers correctly

On my xcode project, I added the dylib of freetype to the project Link Binary phase.
I ensure /usr/local/include and /usr/local/lib are in search paths in the build settings.
I then include:
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
But i get an error that freetype.h was not found. Any ideas? I tried including <freetype2/freetype.h> directly, but that leads to more compile problems with include paths in other freetype files.
Looking at the demo programs in "freetype2-demos", I see:
#include <ft2build.h>
#include FT_FREETYPE_H
Also, I think you need your compiler command-line to include -I (path to freetype includes).
For example...
g++ -I (...)/freetype2 myfile.cpp
Here are the instructions. The suggestion there is to compile with something like...
g++ $(freetype-config --cflags) myfile.cpp
...which, if you system is configured correctly, will incorporate the -I option that I previously mentioned.

Instruct compiler to ignore header prefix found in a #include

[As Cornstalks explained below, I'm trying to strip a header prefix that's used in an #include. So it appears this question is not a duplicate of How to make g++ search for header files in a specific directory?]
I'm making some changes to a library. I have the library locally, so its not installed in its customary system location.
I have a test source file and its sided-by-side with the library. The test file has a bunch of includes like:
#include <foo/libfoo.h>
And it also has a bunch of customary includes, like:
#include <iostream>
I'm trying to compile the test file using:
$ g++ ecies-test.c++ -I. -o ecies-test.exe ./libcryptopp.a
And (the space between -iquote . does not appear to make a difference):
$ g++ ecies-test.c++ -I. -iquote . -o ecies-test.exe ./libcryptopp.a
The problem I am having is I don't know how to tell g++ that <foo/libfoo.h> means "./libfoo.h". Effectively, I'm trying to strip the prefix used to include the header. I've looked in the manual under 2.3 Search Path, but it does not really discuss this scenario.
I have about 60 extra test files I use for the library. And each has 10 or 20 includes like this. So I can't go through and change #include <foo/libfoo.h> to #include "./libfoo.h" in 500 or 600 places.
I tried #rici's work around by creating the fictitious directory structure, but it broke GDB debugging. GDB cannot find symbols for class members, so I can't set breakpoints to debug the code I am attempting to modify.
How do I tell the compiler to look in PWD for system includes?
Below is a typical error. ECIES_FIPS is in my local copy of the library.
$ g++ -DNDEBUG=1 -g3 -Os -Wall -Wextra -I. -iquote . ecies-test.c++ -o ecies-test.exe ./libcryptopp.a
ecies-test.c++:29:17: error: no member named 'ECIES_FIPS' in namespace
'CryptoPP'
using CryptoPP::ECIES_FIPS;
~~~~~~~~~~^
ecies-test.c++:44:5: error: use of undeclared identifier 'ECIES_FIPS'
ECIES_FIPS<ECP>::Decryptor decryptor(prng, ASN1::secp256r1());
^
ecies-test.c++:44:16: error: 'ECP' does not refer to a value
ECIES_FIPS<ECP>::Decryptor decryptor(prng, ASN1::secp256r1());
^
/usr/local/include/cryptopp/ecp.h:30:20: note: declared here
class CRYPTOPP_DLL ECP : public AbstractGroup<ECPPoint>
...
In case it matters:
$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin12.6.0
Thread model: posix
There is no option which tells gcc to ignore directory prefixes in include paths. If your program contains #include <foo/header.h>, there must be some path_prefix in the include list such that path_prefix/foo/header.h resolves to the desired file.
While you cannot configure gcc to ignore the foo, you can certainly modify the filesystem as you please. All you need is that there be somewhere a directory foo which maps onto the directory where the header files are stored. Then you can add the parent of that directory to the search path.
For example:
mkdir /tmp/fake
ln -s /path/to/directory/containing/header /tmp/fake/foo
gcc -I /tmp/fake ... # Ta-daa!
Using the -I option to add the current folder as an include directory, you could create a folder called "foo" in the current directory and put your libfoo.h file inside.
Obviously, this doesn't strip the "foo" prefix in your #include, but it is a workaround.
I have about 60 extra test files I use for the library. And each has 10 or 20 includes like this. So I can't go through and change #include to #include "./libfoo.h" in 500 or 600 places.
If the above criteria is just a matter of convenience, then a tool like sed can be used to do all the work. Something like
$ sed -i 's/\(^\s*#include\s*[<"]\)foo\/\([^>"]*[>"]\s*$\)/\1\2\t\/\/ This line was replaced/' *
will replace all the occurrences of #include <foo/file.h> with #include <file.h> (you might have to adjust it slightly, I'm on a Windows machine at the moment and can't test it). This will work if all the files are in the PWD. If there is a more complex file structure, then it can be used in conjunction with grep and xargs.
NOTE: Make sure that the svn directories are ignored when using.

compile <json/json.h> in eclipse using c++

I'm trying to include and compile
#include <json/json.h>
However even though I've installed it and included it on the project settings it wouldn't find the path.
here's what I've so far done:
path to libjson:
/usr/include/jsoncpp-src-0.5.0
options in eclipse gcc c++ compiler:
-Ijson_linux-gcc-4.5.2_libmt -O0 -g3 -Wall -c -fmessage-length=0 -ljson_linux-gcc-4.5.2_libmt
libraries in gcc c++ linker:
-L/usr/include/jsoncpp-src-0.5.0/include/
Anything else I forgot to do to make it work?
try adding -I/usr/include/jsoncpp-src-0.5.0 to compiler options
-L indicates where to find shared libraries (e.g. .so)
-I is the search path for the header files.
If the problem occurs during compilation (json.h not found), then you have indicated the wrong -I
If it occurs during linking (symbol not found), then you have indicated a wrong -L, a wrong -l, or forgot to run ldconfig

Using the c++ Boost regex libraries in Arch Linux

I'm having trouble building a simple c++ program that tests out regex's from the boost library. The problem that I'm having is occurring in the linking stage and I don't know enough to fix the error on my own.
In the .cpp program that uses regexes I used the following include line.
#include <boost/regex.hpp>
I don't know enough to figure out what command I should use to build the program using g++. I tried using the following command line (and variations of it) to build the program.
g++ -I/usr/include/regex -L/usr/lib -llibboost_regex main.cpp -o regex_test
Other information that might be relevant:
Operating system: Arch linux
g++ version: 4.6.2 20120120 (prerelease)
Any help would be appreciated.
Assume you have installed Boost with the boost and boost-libs packages,
The header <boost/regex.hpp> should exist in /usr/include/boost/regex.hpp. You don't need to use any -I flags since /usr/include should be included by default.
You shouldn't need the -L flag either since /usr/lib should also be included by default when linking.
When using the -l flag to link with a library libfoo.so, the leading "lib" part should be removed.
The command line should therefore be:
g++ main.cpp -o regex_test -lboost_regex