How to install clang header files? - c++

I have clang installed on my MacOS (in /usr/bin/clang ) which I think comes installed by default on Mac, however, when I try to include clang header files in a script, it says they are not found
Example.cpp:1:10: fatal error: 'clang/Driver/Options.h' file not found
Question: is it necessary (and possible, if so, how) to install the header files when clang is already installed and built on the MacOS system (or does clang itself need to be reinstalled at the same time as all the desired development tooling packages and their header files are installed)?
#include "clang/Driver/Options.h"
#include "clang/AST/AST.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Rewrite/Core/Rewriter.h"

When you use double quotes for including the libraries it will search the current directory which your c/cpp file or application resides in. Try with < and > or compile with the -I option

The question asked if it was necessary and possible to install the header files on MacOS which comes with clang installed already. The desired header files weren't installed and in order to install them it is possible to clone the repo and build llvm and clang (as described in the llvm getting started guide http://llvm.org/docs/GettingStarted.html) , so that it's in effect installed twice on the system.

Related

Xcode 11.1: iostream' file not found

I just updated my MacBook Pro to macOS Catalina 10.15, and tried to compile and run a C++ command line program, but I had a problem which didn’t exist on previous versions;
This is simply the code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!\n";
return 0;
}
The code compiles and outputs the expected, but still the Xcode says:
fatal error: 'iostream' file not found
I tried changing the Build Settings/C++ Standard Library to libstdc++, but a warning says:
warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead
And the same iostream error still exists.
I'm compiling from the command line, and none of the answers listed here (or elsewhere) worked for me.
What does seem to work (so far) is to add the following to .profile or whatever script your terminal uses to start up: (zsh, csh, bash, etc.)
export C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include
export CPLUS_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include
You will probably have to change MacOSX10.15.sdk whenever you upgrade your operating system.
C_INCLUDE_PATH and CPLUS_INCLUDE_PATH are options for the clang toolchain rather than MacOS environment, so hopefully this solution will work long-term, unlike xcode-select --install (which won't fix the include directories on an upgrade) or ln -s ... /usr/include (which is now forbidden by System Integrity Protection).
I had the same problem and used the following youtube video to fix it.
https://www.youtube.com/watch?v=hrPm7tWC-BI&feature=youtu.be
or you can follow this path. Make sure to include the quotation marks
Project - Build Settings - Search Paths - Headers Search Paths, and add the following path:
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/"
So, I restarted my laptop and everything seems to be fine right now, thanks for those who tried to help.
libstdc++ is not OK for Xcode Build & Compile time,
libstdc++ is OK for iPhone Run Time
From answer recommended by #Alan Birtles
libstdc++ Support was removed from the iOS 12.0 Simulator runtime, but
it remains in the iOS 12.0 (device) runtime for binary compatibility
with shipping apps.
I encountered this when declaration in .hpp file.
#include <iostream>
#include <string>
OK with
#ifdef __cplusplus
#include <iostream>
#include <string>
// usage code
#endif
I tried a fresh Catalina install with Xcode. I copied and pasted your code into "test.cpp" and then ran:
clang++ test.cpp
in the same directory as the "test.cpp" file from Terminal. The result was an "a.out" file which when run:
./a.out
output the required "Hello, World!" result. Hopefully that is of some use (as a point of reference).

Clang Tool Cannot Find Required Libraries on Ubuntu 16.10

I am building a simple C++ Clang tool program called ClangEx using CMake on Ubuntu 16.10 x64.
The project has a single main.cpp file. It's contents are as follows:
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
using namespace clang::tooling;
using namespace llvm;
static llvm::cl::OptionCategory MyToolCategory("my-tool options");
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static cl::extrahelp MoreHelp("\nMore help text...");
int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
}
It builds successfully with CMake but when I use it to analyze an example C++ program, I get the following error:
$ ./ClangEx SandwichBar.cpp --
In file included from /home/bmuscede/SandwichBar.cpp:11:
In file included from /home/bmuscede/SandwichBar.h:14:
In file included from /home/bmuscede/Customers/Sandwich.h:15:
In file included from /home/bmuscede/Customers/../Capital/Recipe.h:14:
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/string:40:
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/bits/char_traits.h:40:
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/bits/postypes.h:40:
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/cwchar:44:
/usr/include/wchar.h:39:11: fatal error: 'stdarg.h' file not found
# include <stdarg.h>
^
1 error generated.
Error while processing /home/bmuscede/SandwichBar.cpp.
I was able to find this bug but installing clang-3.9 doesn't seem to help my situation.
Any advice would be appreciated.
This problem arises sometimes when you have both gcc and clang installed. By default C_INCLUDE_PATH and CPLUS_INCLUDE_PATH are set to search gcc's own include files and don't include clang's include files. And Clang needs the clang specefic include files. To fix try:
export C_INCLUDE_PATH=$C_INCLUDE_PATH:"<clang_include_path>"
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:"<clang_include_path>"
where <clang_include_path> is usually /usr/lib/clang/<version>/include, but can vary based on your installation. On my system, since I built clang from source it's totally different.
If you permanently want to export the two flags, add the same two lines to ~/.bashrc
Hope that helps.

Working with CLang in c++

I have a compilation error in my program related to the included files, in my program I am including this files:
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"
The error message I get when I compile the program using g++ is:
In file included from /usr/include/clang/AST/APValue.h:17:0,
from /usr/include/clang/AST/Decl.h:17,
from /usr/include/clang/AST/RecursiveASTVisitor.h:17,
from FindClassDecls.cpp:2:
/usr/include/clang/Basic/LLVM.h:20:34: fatal error: llvm/Support/Casting.h: No such file or directory
compilation terminated.
I don't have any idea how to solve the problem, and also I am not sure that I installed the CLang library correctly, so can you please tell me how to solve the problem or how to install it correctly on linux (Ubuntu).
It seems that you have the Clang headers installed, but not the LLVM headers (which Clang relies upon). When you are compiling your code, you need to pass the path of LLVM headers with -I to your compiler, as usual.
I'd grab a pre-built Clang+LLVM from the Download page and compile/link against that.
sudo apt-get install libclang-3.8-dev # or libclang-3.9-dev

Missing header files on Mac 10.6.8 stop compilation (SNAP software installation)

I'm trying to install Stanford Network Analysis Project.
I work on MacOS 10.6.8. Yesterday I've downloaded the latest compatible version of Xcode (3.1.4) in order to get the GCC package with a C++ compiler.
When I run the "make" command in my Unix Terminal I get hundreds line of errors.
The first one is about a missing file named "poll.h".
make -C snap-core
g++ -c -std=c++98 -Wall -O3 -I/Xcode3.1.4/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys -I/Xcode3.1.4/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers Snap.cpp -I../glib-core
In file included from Snap.h:8,
from Snap.cpp:4:
../glib-core/base.h:68:24: error: sys/poll.h: No such file or directory
../glib-core/base.h:75:21: error: netdb.h: No such file or directory
../glib-core/base.h:76:25: error: arpa/inet.h: No such file or directory
I've searched on my hard drive where this "poll.h" file was. I can't find it. I can't find netdb.h or inet.h and a few others either.
These files are not the first ones to be included. I'm saying this to make clear that some files are found but some others are missing. This is what actually happen before I encouter the error:
#include <stdint.h>
#include <stdarg.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <signal.h>
#include <sys/poll.h>
As several files are missing I believe I'm missing something big in this issue. Nonethelless I've found out a way to download "poll.h" here. I don't feel comfortable using it... How can I be sure it's the proper file ? What about the other missing files ?
Any suggestion would be really appreciated.
Thanks a lot. Nicolas.

libusb not visible in IDE

I m using Code blocks in Ubuntu Linux. I would like to write programs with the library libusb. I have this file in the system, but the complier in the IDE cannot find this file. I add in "build options" this file, it's not help. What is the solution to this problem?
#include <libusb.h>
Update:
dpkg -L libusb-dev shows following:
/.
/usr
/usr/bin
/usr/bin/libusb-config
/usr/include
/usr/include/usb.h
/usr/lib
/usr/lib/libusb.a
/usr/lib/libusb.la
/usr/lib/pkgconfig
/usr/lib/pkgconfig/libusb.pc
/usr/share
/usr/share/doc
/usr/share/doc/libusb-dev
/usr/share/doc/libusb-dev/changelog.gz
/usr/share/doc/libusb-dev/html
/usr/share/doc/libusb-dev/html/index.html
/usr/share/doc/libusb-dev/html/preface.html
/usr/share/doc/libusb-dev/html/intro.html
/usr/share/doc/libusb-dev/html/intro-overview.html
/usr/share/doc/libusb-dev/html/intro-support.html
/usr/share/doc/libusb-dev/html/api.html
/usr/share/doc/libusb-dev/html/api-device-interfaces.html
/usr/share/doc/libusb-dev/html/api-timeouts.html
/usr/share/doc/libusb-dev/html/api-types.html
/usr/share/doc/libusb-dev/html/api-synchronous.html
/usr/share/doc/libusb-dev/html/api-return-values.html
/usr/share/doc/libusb-dev/html/functions.html
/usr/share/doc/libusb-dev/html/ref.core.html
/usr/share/doc/libusb-dev/html/function.usbinit.html
/usr/share/doc/libusb-dev/html/function.usbfindbusses.html
/usr/share/doc/libusb-dev/html/function.usbfinddevices.html
/usr/share/doc/libusb-dev/html/function.usbgetbusses.html
/usr/share/doc/libusb-dev/html/ref.deviceops.html
/usr/share/doc/libusb-dev/html/function.usbopen.html
/usr/share/doc/libusb-dev/html/function.usbclose.html
/usr/share/doc/libusb-dev/html/function.usbsetconfiguration.html
/usr/share/doc/libusb-dev/html/function.usbsetaltinterface.html
/usr/share/doc/libusb-dev/html/function.usbresetep.html
/usr/share/doc/libusb-dev/html/function.usbclearhalt.html
/usr/share/doc/libusb-dev/html/function.usbreset.html
/usr/share/doc/libusb-dev/html/function.usbclaiminterface.html
/usr/share/doc/libusb-dev/html/function.usbreleaseinterface.html
/usr/share/doc/libusb-dev/html/ref.control.html
/usr/share/doc/libusb-dev/html/function.usbcontrolmsg.html
/usr/share/doc/libusb-dev/html/function.usbgetstring.html
/usr/share/doc/libusb-dev/html/function.usbgetstringsimple.html
/usr/share/doc/libusb-dev/html/function.usbgetdescriptor.html
/usr/share/doc/libusb-dev/html/function.usbgetdescriptorbyendpoint.html
/usr/share/doc/libusb-dev/html/ref.bulk.html
/usr/share/doc/libusb-dev/html/function.usbbulkwrite.html
/usr/share/doc/libusb-dev/html/function.usbbulkread.html
/usr/share/doc/libusb-dev/html/ref.interrupt.html
/usr/share/doc/libusb-dev/html/function.usbinterruptwrite.html
/usr/share/doc/libusb-dev/html/function.usbinterruptread.html
/usr/share/doc/libusb-dev/html/ref.nonportable.html
/usr/share/doc/libusb-dev/html/function.usbgetdrivernp.html
/usr/share/doc/libusb-dev/html/function.usbdetachkerneldrivernp.html
/usr/share/doc/libusb-dev/html/examples.html
/usr/share/doc/libusb-dev/html/examples-code.html
/usr/share/doc/libusb-dev/html/examples-tests.html
/usr/share/doc/libusb-dev/html/examples-other.html
/usr/share/doc/libusb-dev/copyright
/usr/share/doc/libusb-dev/changelog.Debian.gz
/usr/share/doc-base
/usr/share/doc-base/libusb-dev
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/libusb-config.1.gz
/usr/lib/libusb.so
But when I change #include <libusb.h> to #include <usb.h> and try to compile this code:
#include <stdio.h>
#include <stdlib.h>
#include <usb.h>
int main(){
usb_init();
printf("Hello world!\n");
return 0;
}
I get the following error:
undefined reference to 'usb_init'
Seems like libusb-dev is missing.
Or
Execute dpkg -L libusb-dev to list all the files in libusb-dev package to find out what is appropriate include file name and it's location. Most likely you will end up changing your include to:
#include <usb.h>
Also, it is not enough just to add a header to the program - you need to show what library should be linked to use those symbols that are declared in the header. Add following to your compiler options:
-lusb