How to use clangd to find headers of gcc on MacOS - c++

My system is macOS 12.3 21E230 arm64 and I have installed gcc and llvm from homebrew.
I use Neovim as editor and use nvim-lsp via clangd to complete my code, and it works well with clang.
Now I want to use clangd to find headers of gcc and compelete them in my code. I have tried to find answer from https://clangd.llvm.org/, but I haven't find any way to solve this problem.
I tried setting --query-driver, -include-dir, -isysroot, etc.

Using --query-driver seems to be the most promising approach.
If it's not working, please share more details in the form of clangd logs.
Some clangd issues reported my Mac users that might contain helpful discussion are:
https://github.com/clangd/clangd/issues/487
https://github.com/clangd/clangd/issues/1079

Related

Install LLVM in MacOS

I am learning LLVMand C++, but I find it hard to import LLVM into my project.
The first problem is my system has already installed clang and clang++ in usr/bin, but the other LLVM which was installed by brew is in /opt/homebrew/opt/llvm, is ok to delete clang in usr/bin and add opt/homebrew/opt/llvm/bin to env variable?
The second problem is how to link LLVM to my VSCode project, I try link LLVM by
#include "/opt/homebrew/opt/llvm/include/llvm/IR/Constants.h"
but it can't work.
Thanks for any help !!!
Never delete system utilities. Of any kind. For any reason.
Read the information presented to you in brew info llvm. This information was shown to you when you installed it.
Don't #include absolute paths.
Learn how to use compiler/linker flags to tell your compiler where to find headers/libraries.
Read the libc++ documentation to learn how to specify a specific libc++.
Finally, use a tool like cmake to automate this for you.
A homespun makefile might be sufficient.
What you are attempting to do is highly non-trivial. As one of the comments suggests, I would also recommend getting familiar with what's baked in before you try switching to a custom version.

clangd on linux cannot find default headers

I've installed the newest LLVM toolchain from the official PPA, including clang, clang-tools-extra, lldb, lld, libc++ and libc++abi. I'm trying to use clangd with emacs + lsp.
All works fine except clangd cannot find default headers like memory, vector and so on. My code is fine, it is normally compiled using clang. My compile_commands.json also okay.
How to make clangd work?
Edit: removed mention about building llvm+clang from sources since it has been done incorrectly.
I didn't find any info on the internet, so posting the solution here.
Suppose the llvm toolchain is installed in /usr/lib/llvm-8.
The answer is:
sudo ln -s /usr/lib/llvm-8/include/c++/v1 /usr/include/c++/v1
That's it. Good luck.

Xcode tools on Mac support c++11?

I do most of my coding using Qt in C++ and have noticed that I get x86 architecture not found errors when I enable support for C++11 in my Qt .pro files.
I have the latest Ver of Xcode and have developer tools installed. I realize that OSX uses clang but it still seems that I am still stuck with OSX only supporting earlier versions of gcc.
Is there a reason we can't get these updates from Xcode, and if not I guess I need to install these updates myself?
You can use clang with this FLAGS -std=c++11 -stdlib=libc++ to use C++11 features, another similar question can be found here

Using homebrew, gcc and llvm with C++ 11

Here's my problem: I want to use C++11 features provided by either gcc or clang. However, I have these requirements:
I'm using a mac
I'm dependent on a bunch of libraries provided by homebrew (and really don't want to compile them myself). Specifically OSG, which itself is dependent on a ton of other libraries. And boost, though I can always compile that myself.
Homebrew seems to only want to use gcc (please correct me if I'm wrong). I can't find any options to switch to LLVM instead. While I understand that this might be due to the fact that not all libraries are compatible with LLVM yet, this would still be a nice feature for those that are.
The version of gcc that comes pre-installed on a mac of gcc is 4.2. gcc 4.2 doesn't have the c++11 features required. I've installed 4.7 via homebrew, but searches for how to set homebrew to use it all say don't do it (gcc 4.2 on the mac is not the vanilla version, so the 4.7 version I got won't be able to compile some things).
My questions are: Does anyone have any suggestions or fixes they have implemented to get around this problem? Should I give up on Homebrew? Does anyone know if Homebrew has a plan to switch to LLVM in the future? Does anyone have any upgrade-plan for how to deal with these incompatibilities?
I don't see how homebrew can continue to depend on gcc 4.2 in the long run, but haven't found any real discussion on this matter.
The default GCC on Mac is not real GCC of GNU. It's LLVM-GCC in fact, which is a branch of GCC. Several years ago, LLVM-GCC was terminated, and replaced with DragonEgg, which is a GCC plugin to use LLVM as a GCC backend.
LLVM-GCC is just a compiler frontend, whose role is using GCC frontend to translate the source code into LLVM IR[Intro to LLVM 11.3]. Once IR generated, LLVM backend will use it to generate binary code. This step has nothing to do with GCC.
The above goal was fully achieved from 10.7, whose components were all compiled by clang, a frontend provided by LLVM.
But Apple still kept LLVM-GCC and GCC runtime libraries. I guess its purpose might be providing a opportunity to compile some code GCC ONLY.
Now let's answer your questions:
If you want to use C++11 features, use clang++ -stc=c++11 -stdlib=libc++ instead. And clang might have already supported all c++11 features.
If you want homebrew supporting LLVM, it has already supported, at least on backend.
If you want homebrew using clang as a compiler frontend, it depends on homebrew community schedule. For example, you can append --with-c++11 argument to use clang to compile boost.But you cannot use this argument when brew install autoconf. In fact, some components might not be compiled correctly by clang.
If you know it can be compiled by clang but homebrew hasn't supported yet, you have to hack the corresponding ruby script at $HOMEBREW_ROOT/Library/Formula directory. Fortunately, in most of cases, replacing ./configure blablabla with ./configure blablabla CXX=clang++ -stc=c++11 -stdlib=libc++ works well. And by the way, if your hack is successful, please make a pull request to homebrew.
So, try it and have a fun.
I have an OS X Mountain Lion environment and use C++11. In this answer I'll break your requirement for not compiling your own stuff.
I use Homebrew and, I must say, I advise you to give up on depending on it to provide you clang and libc++ and all its formulas built with them.
What I've done, and I like, is
clone llvm, clang and libc++ from repositories.
install to /opt/local and put /opt/local/bin at top on /etc/paths.
build my development stuff with my new clang.
let Homebrew for installing tools like git and things I'll not develop for, just use.
I've followed clang build instructions for installing it to /opt/local.
For libc++, one detail: after running the buildit script, I've symlinked the include directory to /opt/local/lib/c++/v1 (clang on /opt/local looks for this as default directory), and also symlinked the libs to /opt/local/lib/ (but look that binaries will not automatically link to libc++ on /opt/local/lib. You must use install_name_tool for that).
use
clang++ -std=c++11 -stdlib=libc++
you can also install latest gcc from homebrew-dups
brew install [flags] https://raw.github.com/Homebrew/homebrew-dupes/master/gcc.rb
For LLVM, brew install --HEAD llvm. To use clang or a brew-installed gcc, add --with-clang or --with-gcc=gcc-x.x where x.x is the version you want.

is there any way to configure waf to build c++ program in mac os?

I just suffer a problem that the all the things works well on my ubuntu.
However, I want to get things work on my mac, bad thing happens.
it shows the following errors
cc1plus: error: unrecognized command line option "-std=c++0x"
I am total new to mac stuff, I got the xcode 4 installed.
I guess there must be c++0x, but I wonder how can i configure it with waf.
Thanks a lot!!
I’m guessing you’re using GCC supplied with Xcode. That’d be GCC 4.2.1, a rather old version that won’t be updated by Apple in the foreseeable future.
You have essentially two options:
Xcode ships Clang/LLVM besides GCC, so you could use Clang/LLVM instead. That -std=c++0x option is recognised by Clang/LLVM but C++0x is not as fully supported as in recent versions of GCC. The LLVM project keeps a page listing their current C++0x support status.
Use a more recent version of GCC. You can either compile it locally or install it via one the open source package managers available on Mac OS X: MacPorts, Fink, Homebrew. I don’t really know if and which versions of GCC they’re able to build, so check with them first.
As Xcode comes with Clang on mac you can get c++0x support if you configure waf to use Clang.
In your wscript add to configure:
def configure( conf ):
...
conf.env.CXXFLAGS = [ '-std=c++0x', '-stdlib=libc++' ]
conf.env.LINKFLAGS = [ '-std=c++0x', '-stdlib=libc++' ]
....
Then run waf as:
CXX=clang++ waf configure
CXX=clang++ waf build
On Mac you can't go wrong with clang. You'll have to build the compiler yourself (using e.g. gcc-4.2 that you already have). It has -std=c++0x. The support for it isn't complete, but it is growing all the time. On the Mac you might also look at libc++ for C++0x support (combined with clang).