How to install clang in custom location from SVN, making it recognize c++ standard library, on OS X - c++

I try to compile and use clang from svn trunk. I basically try to follow the directions here:
svn co -q http://llvm.org/svn/llvm-project/llvm/trunk llvm
svn co -q http://llvm.org/svn/llvm-project/cfe/trunk llvm/tools/clang
svn co -q http://llvm.org/svn/llvm-project/clang-tools-extra/trunk llvm/tools/clang/tools/extra
svn co -q http://llvm.org/svn/llvm-project/compiler-rt/trunk llvm/projects/compiler-rt
mkdir llvm_build_release
cd llvm_build_release
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$HOME/usr/local -DLLVM_TARGETS_TO_BUILD=host ../llvm
make -j12
make install
Above, I configure clang to be installed in the custom location ~/usr/local since I want to be able to play with it without changing my default environment.
I then create a simple test.cpp:
#include <iostream>
int main(int argc, const char* argv[]){
std::cout << "Hello world\n";
return 0;
}
and try to compile it:
~/usr/local/bin/clang++ test.cpp -o test
but get the error message:
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
(using the system version of clang, the same compilation works fine).
If I manually enter which standard library to use, it does work
~/usr/local/bin/clang++ -std=c++11 -stdlib=libstdc++ -I/usr/include/c++/4.2.1/ -L/usr/lib test.cpp -o test
The question is: How do I configure, compile and install clang from source so that I do not have to enter these standard library settings, but instead can just enter the ordinary ~/usr/local/bin/clang++ test.cpp -o test? I have macports installed, with its version of the standard libraries and the include files, if that helps.

Related

Can't compile c++ program using gcc on Mac after updating to Monterey

*Update:
Regarding the similar question macos-wchar-h-file-not-found, the command line tool switch(xcode-select --install) doesn't exist anymore.
I am running on macOS Monterey system and I am trying to compile a simple hello world .cpp file using the g++-11 commend (I installed gcc using homebrew), I am getting the following error:
/usr/local/Cellar/gcc/11.2.0_3/include/c++/11/cwchar:44:10: fatal error: wchar.h: No such file or directory
44 | #include <wchar.h>
| ^~~~~~~~~
I tried two compile commands:
g++-11 -c hello.cpp -o hell.o
g++-11 -std=c++11 -c hello.cpp -o hell.o
Here is the file I am trying to compile:
//
// main.cpp
// Test
//
// Created by Jiali Zhu on 1/9/22.
//
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
I also had the same problem and in my case the problem was outdated
command line developer tools.
I found out it by running
$ brew doctor
...
Warning: Your Command Line Tools are too outdated.
Update them from Software Update in System Preferences or run:
softwareupdate --all --install --force
If that doesn't show you any updates, run:
sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install
Alternatively, manually download them from:
https://developer.apple.com/download/all/.
You should download the Command Line Tools for Xcode 13.1.
I tried to update them via softwareupdate -l, however this did not give me any update information.
So removed them and reinstalled with xcode-select --install.
After this g++-11 stopped giving me an error about this header.
Now brew config tells me that I have CLT: 13.0.0.0.1.1627064638
P.S.
Also I started the xcode and it asked me whether additional components need to be installed and I said yes.

can not build cpp using clang pre-built binary: file wchar.h not found

I am using MacOS 10.15. Since the clang shipped with MacOS does not include clang-format. I installed another pre-built clang binary from here. I have added the binary file path to my PATH variable.
export PATH="$HOME/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin:$PATH"
I tried to compile a simple program:
#include <iostream>
int main(int argc, char *argv[]) {
std::cout << "Hello world!\n";
return 0;
}
using the following command:
clang++ hello.cpp -o hello
I got the following error:
In file included from hello.cpp:1:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/iostream:37:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/ios:214:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/iosfwd:95:
/Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/wchar.h:118:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
^~~~~~~~~
1 error generated.
I found that wchar.h bundled with this pre-built package is in the following directory:
/Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/include/c++/v1/
So I added the -I flag:
clang++ -I /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/include/c++/v1 hello.cpp -o hello
The error still persists.
If I use clang++ shipped with MacOS, I have no problem compiling the source code:
# the following works without any error
/usr/bin/clang++ hello.cpp -o hello
I have seen post here, here, and here, but the solutions do not apply.
You got clang-format improperly. Reset the system to the state before you installed another pre-built clang binary. Then use Homebrew to install clang-format
brew install clang-format
clang+llvm-10.0.0-x86_64-apple-darwin is not suitable to your Mac. It depends on system frameworks that are not available, so you get the error finding wchar.h in a system framework. When you install clang+llvm-10.0.0-x86_64-apple-darwin you ignore framework dependencies. Homebrew will care about dependencies.

Tensorflow c++ api

I recently tensorflow cpp api from a way below(on MacOS Catalina ):
1. I first Install Bazel 0.23.0
2. download tensorflow:
$ git clone https://github.com/tensorflow/tensorflow
cd tensorflow # cd to the top-level directory created
$ ./configure
bazel build //tensorflow:libtensorflow_cc.so
# build C library
$ bazel build //tensorflow:libtensorflow.so
After install I run the code below:
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/platform/env.h"
int main(){
return 0;
}
with. command below:
g++ -I/usr/local/Cellar/libtensorflow/2.1.0_1/include -L/usr/local/Cellar/libtensorflow/2.1.0_1/lib -ltensorflow -ltensorflow_cc -ltensorflow_framework -std=c++11 main.cc -o main.o
but I get Error below:
/usr/local/Cellar/libtensorflow/2.1.0_1/include/tensorflow/core/framework/device_attributes.pb.h:17:2: error:
This file was generated by an older version of protoc which is
#error This file was generated by an older version of protoc which is
^
/usr/local/Cellar/libtensorflow/2.1.0_1/include/tensorflow/core/framework/device_attributes.pb.h:18:2: error:
incompatible with your Protocol Buffer headers. Please
#error incompatible with your Protocol Buffer headers. Please
I used libprotoc 3.11.4.
what should I do? what is wrong?

Difficulty linking Boost 1.60 on OS X after installing via Homebrew

I have a fresh install of OS X 10.11.4 that I immediately installed the Xcode toolchain on, then Homebrew, then Boost 1.60. In order to test that everything had gone well, I wrote the following code on my Desktop.
#include <iostream>
#include <boost/filesystem.hpp>
int main() {
boost::filesystem::path new_directory("hello");
boost::filesystem::create_directory(new_directory);
}
I then attempted to compile it as I usually have done with the following command.
$ clang++ test.cpp -o test -lboost_system -lboost_filesystem
I received the following error.
test.cpp:3:10: fatal error: 'boost/filesystem.hpp' file not found
#include <boost/filesystem.hpp>
This is how I have always compiled projects that link Boost in the past. I'm assuming that I have probably forgotten a step along the way that allows clang to search a specific path to dynamically link the libraries. What should I change in order for this compilation command to work?
For me, boost has been compiled and installed into a subdirectory of my home directory, so you'll need to modify the paths as appropriate for your homebrew installation:
flags="-std=c++1z -I/${HOME}/local/include -L${HOME}/local/lib -lboost_filesystem -lboost_system"
c++ ${flags} -o jared jared.cpp
First get the location of boost by doing the following:
brew info boost
From the image above, you can see that my location is
/usr/local/Cellar/boost/1.66.0
Then, to compile, use the following:
c++ -I /usr/local/Cellar/boost/1.66.0 main.cpp -o boost

How to install CLang using precompiled binaries?

How do I install CLang on Ubuntu, using precompiled binaries of CLang that I downloaded?
Here's how I downloaded CLang: "LLVM Download Page" -> "Download LLVM 3.2" -> "Clang Binaries for Ubuntu-12.04/x86_64" ( http://llvm.org/releases/3.2/clang+llvm-3.2-x86_64-linux-ubuntu-12.04.tar.gz .)
Then, I expanded the archive into a folder on my Ubuntu 12.04 LTS 64-bit machine. The contents of the expanded folder look like this:
$ ls clang+llvm-3.2-x86_64-linux-ubuntu-12.04
bin docs include lib share
Question: What do I do next? Do I have to copy these into some folders myself, and if so, which ones exactly? Most instructions I found online are for building CLang from source, which doesn't apply here.
I am a newbie to most of these tools. I created a basic hello-world C++ program, and was able to compile and run it, using GCC and autotools. Now, I want to compile the same program with CLang.
You can follow the same step as mentioned in https://askubuntu.com/questions/89615/how-do-i-install-llvm-clang-3-0
using GNU tar:
wget <clang-binaries-tarball-url> # or `curl -O <url>`
tar xf clang*
cd clang*
sudo cp -R * /usr/local/
If your tar isn't GNU and
the archive you get is .tar.gz, you can use tar -xzf;
if you have .tar.xz archive, you can use tar -xJf;
for .tar.bz2 archive, you can use tar -xjf.
Assuming you compiled your program with g++ hello.cpp
The equivalents of gcc and g++ are clang and clang++ accordingly. They are found in the bin folder.
It doesn't matter where you place the folders of clang, what matters is you don't move them later. So place them somewhere (I prefer $HOME and I'll assume this for the next)
Then:
Prepend it to $PATH variable
export PATH=~/clang+llvm-3.2-x86_64-linux-ubuntu-12.04/bin/:$PATH
Make this permanent by adding it to ~/.bashrc
echo "export PATH=~/clang+llvm-3.2-x86_64-linux-ubuntu-12.04/bin/:\$PATH" >> ~/.bashrc
Now you can do clang++ hello.cpp
I would like to install clang in /home/s. i.e.,
/home/s
bin
lib
include
...
I did the following in Ubuntu:
wget <clang-binaries-tarball-url>
sudo tar -xf <clang+llvm-..tar.xz> --strip-components=1 -C /home/s
# Set the path environmental variable
export PATH=/home/s/bin:$PATH
# Tell ldconfig about new shared library in /home/s/lib
cd /home/s
cat > libs.conf << "END"
/home/s/lib
END
sudo mv libs.conf /etc/ld.so.conf.d/libs.conf
sudo ldconfig
To test it:
clang --version
The output is:
clang version 7.0.0 (tags/RELEASE_700/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/s/bin
Let's test C++17 Filesystem withex1.cpp
#include <iostream>
#include <filesystem>
int main() {
for(auto &file : std::filesystem::recursive_directory_iterator("./")) {
std::cout << file.path() << '\n';
}
}
Compile it
clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic ex1.cpp -o ex1 -lc++fs
Run it
./ex1
The output:
"./ex1"
"./ex1.cpp"