I've a CMake project containing C and C++ files, and I have a CI set up. In the CI, I want to build my project with some additional flags to make the compiler stricter, in particular -Wall -Wextra -Werror -pedantic.
To configure the project, I'm currently repeating myself by doing this:
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS="-Wall -Wextra -Werror -pedantic" -DCMAKE_CXX_FLAGS="-Wall -Wextra -Werror -pedantic"
As I don't want these flags baked into the CMakeLists file (since this strictness is only wanted in the CI), I'm setting the flags on the configure command in the CI.
Is there any way to rewrite this command in a way where I don't need to repeat the flags?
Note: CMAKE_CPP_FLAGS does not work.
This is the exact use case for which CMake presets were invented. Please do not hard-code -Werror in your build. It is a liability for your end users who might not use the exact same compiler as you. I wrote another answer on a related question that goes into more detail.
Here's an example CMakePresets.json:
{
"version": 4,
"cmakeMinimumRequired": {
"major": 3,
"minor": 23,
"patch": 0
},
"configurePresets": [
{
"name": "ci-gcc",
"displayName": "CI with GCC",
"description": "CI build with GCC-compatible compiler",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_C_FLAGS": "-Wall -Wextra -Werror -pedantic",
"CMAKE_CXX_FLAGS": "-Wall -Wextra -Werror -pedantic"
}
}
]
}
Then just configure your CI to build with cmake --preset ci-gcc && cmake --build build from your source directory.
I built wxWidgets without any errors with make and did make install and now I have the installation in /usr/local/include/wx-3.1/. Both wx/wx.h and wx/setup.h are present in that folder. I'm using the latest VS Code with the latest Ubuntu 20.04.
c_cpp_properties.json file:
"configurations": [
{
"name": "Linux",
"includePath": [
"/usr/local/include/wx-3.1/",
"${default}"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu18",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
main.cpp:
#include "wx/wx.h"
int main(){}
Compiling gives the following error:
g++ -std=c++17 -Wall -Wextra -g -c -o src/main.o src/main.cpp
src/main.cpp:1:10: fatal error: wx/wx.h: No such file or directory
1 | #include "wx/wx.h"
| ^~~~~~~~~
compilation terminated.
The weird thing is that VS Code Intellisense detects that wx is included and there is no error or red underline (removing the include folder from includePath makes VS Code display an error).
What's the problem here? Everything seems to be correctly included.
in addition you will need to point the linker to the libraries to use with the -L option and use -l (lowercase L) with every library you will need.
Example:
-L/usr/local/lib -lwxcore-3.1 -lwxbase-3.1
Check the library names there and use them without lib prefix.
Better yet - try to run wx-config --libs and use the output for the linker options in your IDE.
Also, for complete set of options to the compiler use the output of wx-config --cxxflags
You must configure the IDE to call the compiler with the right options. The right options for wxWidgets projects must include the output of `wx-config --cxxflags` for C++ compiler and `wx-config --libs` for the linker.
Do not hardcode any paths in your project options, using wx-config is both simpler and more portable.
I'm trying using package manager nix for my C++ project.
default.nix:
{ pkgs ? import <nixpkgs> {} }:
let
stdenv = pkgs.stdenv;
in rec {
myProject = stdenv.mkDerivation {
name = "lynx";
version = "dev-0.4.0";
buildInputs = [
pkgs.cmake
pkgs.gtest
];
};
}
I built the project in its directory by using cmake without any problems. Then I exported the project as an Eclipse project (I did it under nix-shell):
cd ..
mkdir lynx_eclipse
cd lynx_eclipse
cmake -G"Eclipse CDT4 - Unix Makefiles" -D PLATFORM:STRING="posix" -DCMAKE_BUILD_TYPE=Debug ../lynx/
Having opened the exported project in Eclipse I found that include paths are fine and point to /nix/store/*. But when I try to build the project I have an error:
gtest/gtest.h: No such file or directory
I see that Eclipse doesn't add nix'es paths to the compiler flags:
/nix/store/ix03iknfgyrx7421fppjdczd9r4sw7pz-gcc-wrapper-5.3.0/bin/g++ -I/home/ubuntu-pc/dcs/lynx/inc -I/home/ubuntu-pc/dcs/lynx_eclipse/inc -I/home/ubuntu-pc/dcs/lynx/test/./inc -std=c++11 -static-libstdc++ -g -o CMakeFiles/test_utils.dir/utils_test.cpp.o -c /home/ubuntu-pc/dcs/lynx/test/utils_test.cpp
That could be reason of the problem but I don't know how to change Eclipse's behaviour.
Judging by the number of the views the problem is very specific. However, I found the solution somehow.
After the export to Eclipse project I made builder.sh file:
export NIX_PATH=nixpkgs=/home/xxxx/.nix-defexpr/channels/nixpkgs
/home/xxxx/.nix-profile/bin/nix-shell default.nix --run "make $1"
Then I set the file as a build command in C\C++ General -> C\C++ Make Project
bash ${project_loc}/build.sh
That's all.
I've written the following groundbreaking GPU-powered application:
int main() { return 0; }
and I'm trying to build it using CMake. Here's my CMakeLists.txt file:
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} " -std=c++11" )
find_package(CUDA QUIET REQUIRED)
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} " -std=c++11")
cuda_add_executable(a a.cu)
With this file, and for some reason, the compilation fails; it looks like (although I'm not sure) the reason is that CMake is having NVCC use the CUDA include directory twice. The result:
/home/joeuser/opt/cuda/bin/nvcc -M -D__CUDACC__ /home/joeuser/src/kt3/a.cu -o /home/joeuser/src/kt3/CMakeFiles/a.dir//a_generated_a.cu.o.NVCC-depend -ccbin /usr/bin/ccache -m64 --std c++11 -Xcompiler ,\"-g\" -std=c++11 -DNVCC -I/home/joeuser/opt/cuda/include -I/home/joeuser/opt/cuda/include
nvcc fatal : redefinition of argument 'std'
CMake Error at a_generated_a.cu.o.cmake:207 (message):
Error generating
/home/joeuser/src/kt3/CMakeFiles/a.dir//./a_generated_a.cu.o
You might be asking why my MWE is not more terse. Why do I need those option-setting lines above if I don't use C++11 anyway? Well, if I remove them, I still get the double-include, but have an earlier failure involving ccache about which I will probably ask another question.
So is the problem actually the double-include, or is it something else? And what should I do?
Additional information:
I don't have root on this machine.
CMake version: 3.3.2 .
Distribution: Fedora 22 (sorry, I can't help it; but I also seem to be getting this on Debian Stretch as well).
CUDA install location: $HOME/opt/cuda , and its binary directory is in $PATH.
On another system, with a different configuration and distro (Fedora 20, CUDA 7.5 but in another local dir, possibly other differences) I do not get this behavior.
I think the problem is that the nvcc flags are propagated to your c/c++ compiler, so some compiler arguments are effectively passed twice. Try using SET(CUDA_PROPAGATE_HOST_FLAGS OFF)
I've tried to compile simple hello world on Fedora 20 with Clang, and I get the following output:
d.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
I don't have any idea how to resolve it.
This is because g++ is not installed, so libstdc++ is not present.
You can install g++, or if LLVM is preferred, install LLVM libc++ and specify that you want to use it, like so:
sudo apt-get install libc++-dev
clang++ -stdlib=libc++ <rest of arguments>
You may wish to link /usr/bin/c++ to the default compiler:
ln -s /usr/bin/c++ /usr/bin/clang++-libc++
and then compile simply using
$ c++ <args_as_usual>
Point 3 solved the problem for me.
1.
Had the same issue, fedora 21::clang 3.5.0:
clang++ -std=c++14 -pedantic -Wall test_01.cpp -o test_01 -v
2.
ignoring nonexistent directory "/usr/lib/gcc/i686-redhat-linux/4.9.2/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/bin/../lib/clang/3.5.0/include
/usr/include
End of search list.
test_01.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
3.
sudo yum install gcc-c++
4.
#include "..." search starts here:
#include <...> search starts here:
/bin/../lib/gcc/i686-redhat-linux/4.9.2/../../../../include/c++/4.9.2
/bin/../lib/gcc/i686-redhat-linux/4.9.2/../../../../include/c++/4.9.2/i686-redhat-linux
/bin/../lib/gcc/i686-redhat-linux/4.9.2/../../../../include/c++/4.9.2/backward
/usr/local/include
/usr/bin/../lib/clang/3.5.0/include
/usr/include
/usr/lib/gcc/i686-redhat-linux/4.9.2/include
End of search list.
One of the reason could be this:
clang doesn't have its own header libraries for c++, so it is pointing towards gcc's library folder to access header files. You can check it out and confirm it by using command
clang -v
This will print its version and the selected gcc installation which will help you to know which version of gcc it is using. For me it was,
Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;#m64
Selected multilib: .;#m64
Now as it can be seen that it has selected gcc's 12th version which I didn't had installed and thus the error because it can't find one(don't know how it selected that version even though I haven't installed it). But the solution is to install g++ nth version. You can do that by following command where n is the version number that's selected by gcc and you're good to go.
sudo apt install g++-n
I got this answer after digging alot and found this blog clangmissingheaders
Looks like you should provide your clang build with -stdlib option. One of -stdlib=libc++ or -stdlib=libstdc++ will probably work.
There are more details on your subject:
When is it necessary to use the flag -stdlib=libstdc++?
-stdlib=libstdc++ solved it for me. Here is my complete tasks.json config:
{
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "clang++",
"args": [
"-std=c++11",
"-stdlib=libstdc++",
"hello.cpp",
"-o",
"hello.out",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
I had a similar issue after updating using Pop!_OS 22.04 LTS x86_64.
OUTLINE OF ISSUE:
Compilation worked just fine so gcc and g++ was working perfectly but the lsp clangd was just throwing ERROR in my editor (neovim) for lines containing anything involving iostream.
Here was my path to refuge:
Created a env (environment) variable that clangd looks for called CLANGD_FLAGS and assigned it the path to my c++ 11 path and putting this into my .bashrc file like so:
code added to my .bashrc:
export CLANGD_FLAGS="-I/usr/include/c++/11"
NOTE:
your path may vary obviously.. Just be sure to precede your path with the -I option.
you can also just link the path to a compile_commands.json file if you desire more settings or specifics
ex:
export CLANGD_FLAGS="--compile-commands-dir=/path/to/compile_commands.json --flag1 --flag2"
This allowed me to access <iostream> globally and avoid writing the compile_commands.json for each new program or project.
For anyone Googling this & have a MacOS, here's the solution I found:
Add the following to your .bashrc/.zshrc file
export CPLUS_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
This should fix it.
Make sure you have the libstdc++ installed that corresponds to the latest verison of gcc installed. Clang seems to identify the latest gcc installation and only look in the appropriate directories for that version.
I've tried to reinstall command line tools and the "ln" command, but it still cannot be fixed.
And after trying almost every other solution on the website, this still cannot be fixed. But the thing is going to be clear: the compiler attempted to find the header files in /usr/include, but although with installed command line tools, there is no this folder at all.
Maybe the best straightforward approach for us is installing the Xcode and coding in this IDE, or other compilers, but not the most lower-cost situation.
Mac has a built-in Clang, we do not need to install extra compilers. Here are the steps.
We can check the CommandLineToos folder, if you haven't installed it, try this command to install it in advance.
xcode-select --install
In the CommandLineTools folder, we may check the route of SDKs, which is /Library/Developer/CommandLineTools/SDKs
/Library/Developer/CommandLineTools/SDKs
We may use MacOSX.sdk, for me, it is also MacOSX12.0.sdk, to find the headers. The C basic headers is found at /Library/Developer/CommandLineTools/SDKs/MacOSX12.0.sdk/usr/include.
/Library/Developer/CommandLineTools/SDKs/MacOSX12.0.sdk/usr/include
But it doesn't contained with C++ basic headers, the C++ basic headers can be found at /Library/Developer/CommandLineTools/usr/include. We can find this route with command g++ -v in the terminal as well.
g++ -v
So the solution will be obvious, type the follow commands in terminal.
Open the bash_profile.
open ~/.bash_profile
Add this.
export C_INCLUDE_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX12.0.sdk/usr/include
export CPLUS_INCLUDE_PATH=/Library/Developer/CommandLineTools/usr/include
Source it.
source ~/.bash_profile
This error will be fixed.
In my case, clang was using version 12 of GCC installation, but I was missing the libstdc++ for that version:
$ clang -v
Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Fixed it like this:
sudo apt install libstdc++-12-dev
TL;DR: If you don't have sudo access and are using conda, you might want to do:
conda install gxx -c conda-forge
In my case I installed clang++ using conda install clangxx thinking it would install the required dependencies but turns out I was missing libstdgxx-devel. I guess it's one of the dependencies of gxx so it gets installed as a side effect.