Using clang-tidy to analize embedded GCC project - c++

I'm trying to use clang-tidy to parse my project, compiled by arm-none-eabi-g++.
Unfortunately, clang-tidy is not able to find compiler headers, even when given the include path to them.
My compile_commands.json is
[
{
"directory": "C:/LMA/repo/clang-tidy",
"arguments": [
"arm-none-eabi-c++",
"-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1",
"-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1/arm-none-eabi/arm/v5te/hard",
"test.cpp"
],
"file": "./test.cpp" } ]
And the example test.cpp file is:
#include <cstdint>
#include <cstdlib>
int test()
{
int temp;
return 0;
}
Clang-tidy shows error:
C:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1\cstdlib:75:15: error: 'stdlib.h' file not found [clang-diagnostic-error]
#include_next <stdlib.h>
So, it properly finds and includes cstdlib, however it is not able to find stdint.h, which is located in the exactly same folder. What's even more irritaiting, it does not include stdlib.h, even when I add
-include C:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1/stdlib.h
to compiler arguments in order to force preinclude.
Any suggestions how to fix this issue are very much appreciated.

This is a bit old, but I was having a similar issue so I figured I'd report an answer here in case anyone else comes across this while searching. It appears clang doesn't know where to find the standard library headers when compiling using arm-none-eabi. I succeeded by simply adding them.
In your case you're still missing the C headers directory, so you need to add this:
C:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/
(assuming the directory structure is similar to mine).
So your compile_commands.json would be:
[ {
"directory": "C:/LMA/repo/clang-tidy",
"arguments": [
"arm-none-eabi-g++",
"-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include",
"-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1",
"-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1/arm-none-eabi/arm/v5te/hard",
"test.cpp"
],
"file": "./test.cpp"
} ]
You should also have arm-none-eabi-g++ instead of arm-none-eabi-c++ I believe.

Related

#include errors detected for eigen library

I have been trying to use the Eigen library for c++ in VS code. I have seen various answers explaining how to solve the problem. I have opened the c_cpp_properties.json. I have my Eigen library :
But my MinGW shows different files from what I have seen in other answers and tutorials.
Error in code:
After this, what should I do? I tried adding the include path, but I cannot solve this.
Any help is highly appreciated.
You can move the whole Eigen Library to an included folder in IncludePath.
This is an example:
settings.json
"C_Cpp.default.includePath": [
"${workspaceFolder}/**",
"C:\\TDM-GCC-64\\include",
"C:\\TDM-GCC-64\\lib\\gcc\\x86_64-w64-mingw32\\10.3.0\\include",
"C:\\TDM-GCC-64\\lib\\gcc\\x86_64-w64-mingw32\\10.3.0\\include\\c++"
]
For you:
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:\\MinGW\\include",
"C:\\MinGW\\lib\\gcc\\x86_64-w64-mingw32\\{Your GCC Version}\\include",
"C:\\MinGW\\lib\\gcc\\x86_64-w64-mingw32\\{Your GCC Version}\\include\\c++"
]
}
]
You can put it in any of the folders like this.

Include a *.cpp file

Currently, I try to bazelize a library. Let's call this library lib_foo.
The setup is similar to this minimal example. My main application tries to use a function form lib_foo.
main.cpp:
#include "lib_foo/header.h"
#include <iostream>
int main() {
std::cout << foo<2>() << std::endl;
}
lib_foo/header.h:
#pragma once
#include "impl.inc" // works
lib_foo/impl.inc:
template <int number>
int foo() {
return number;
}
BUILD.bazel:
cc_library(
name = "lib_foo",
srcs = [
"lib_foo/header.h",
"lib_foo/impl.inc",
],
)
cc_binary(
name = "HelloWorld",
srcs = [
"main.cpp",
],
deps = [":lib_foo"],
)
There is also an empty WORKSPACE file.
If I run bazel run //:HelloWorld everything works as expected.
If I rename the file impl.inc to impl.cpp (and change this accordingly in BUILD and include file, etc.). I run in the following problem (on Ubuntu 20.04):
In file included from main.cpp:1:
lib_foo/header.h:3:10: fatal error: impl.cpp: No such file or directory
3 | #include "impl.cpp"
| ^~~~~~~~~
Also replacing the include path with a relative one to the WORKSPACE file does not help here.
It seems that it is a general problem to include cpp files in Bazel. Since the third party library I try to bazelize does include several times cpp files I wonder what a proper workaround here is.
Any ideas?
(I am using Bazel 3.7.2 on Ubuntu 20.04)
The normal workaround is to put includable cpp files in the textual_hdrs attribute of cc_library.

Visual Studio Code - Include Path Problems Header Files C++ (MinGW)

I am pretty new to programm in C++ so please don't judge my problems with setting up my "Visual Studio Code" environment. I am trying to use the blaze math packages to solve quadratic programming problems. With the MinGW GCC I can compile the testfiles of blaze successfully via cmd and thus I want to use the GCC for VS Code.
OS: Windows 10.0.19041
GCC: gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 7.3.0 || (cmd: gcc --version)
VS Code Extensions: C/C++ 0.27.1
Firstly I went through the VS Code tutorial for MinGW:
https://code.visualstudio.com/docs/cpp/config-mingw
This worked fine so I can easily compile my helloworld.cpp. The resulting tasks.json file looks like this
tasks.json.
As my package manager (for blaze or other packages) i use the vspkg-git:
https://learn.microsoft.com/en-us/cpp/build/vcpkg?view=msvc-160
Thus I am programming on Windows I can't use the "integrate" command to add the path to the includepath. So I have to do this manually.
My packages are in the folder with the absolute path
C:\Users\Johannes\Desktop\Masterthesis\vcpkg\vcpkg\packages
So I added the path on the "c_cpp_propertier.json"-file
{
"configurations": [
{
"name": "GCC",
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"includePath": [
"${workspaceFolder}",
"C:/Users/Johannes/Desktop/Masterthesis/vcpkg/vcpkg/packages/**"
],
"compilerPath": "C:/Program Files/mingw-w64/x86_64-7.3.0-posix-seh-rt_v5-rev0/mingw64/bin/g++.exe",
"browse": {
"path": []
}
}
],
"version": 4
}
In the folder are several packages therefor I added the "/**" at the end of the path to enable the recursive search for header files.
My 'helloworld.cpp' file looks like this
#include <iostream>
#include <vector>
#include <string>
//#include <blaze/Math.h>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the
C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
My problem is that when I try to include a header-file from this path for example Math.h Visual Studio throws an Error
blaze/Math.h: No such file or directory
But when I right-click on the include and click "Go to Definition" VS Code opens the file. Optionbar and the opened file.
The Log-Diagnostics gives that information.
I guess someone that has experience with MinGW and additional packages in VS Code will solve that problem very simple, but I have read nearly every thread about those problems and didn't find anything matching mine.
Okay, i got the answere. The include path of the "c_cpp_properties.json" file is only for IntelliSense. This means that Visual Studio Code will find this packages and IntelliSense will suggest you the available headers from the pathes. This does not mean that the compiler can find these pathes. Therefor you have to add the pathes to the "tasks.json" file. As you can see above in the photo of the "tasks.json" file, there is a field called "args" which means "arguments". Those are the compiler arguments. You have to add the Path there too in Format "-I","C:/PathYouWishToAdd". This works fine!!

How to setup sublime 3 build for C++ with multiple files

First of all, I'm a total noob on SO, so please go easy on me :)
That being said, my question might be really easy to answer:
How would I use sublime text 3 for c++ with multiple files (header/implementation files)
I've been trying some build systems provided by other people,
this is the one I'm currently using and seems to work just fine for compiling single .cpp files but is telling me some errors which I don't understand
here is my build config using g++
{
"cmd": ["g++.exe", "-std=c++14", "-o", "$file_base_name", "$file", "&&", "start", "cmd", "/c", "$file_base_name & echo. & echo. & pause"],
"shell": true,
"selector": "source.c++"}
here is my main.cpp:
#include <iostream>
#include <string>
#include "num.h"
using namespace std;
int main(){
Num n(35);
cout << n.getNum() << endl;
return 0;}
here is my num.h file:
#ifndef NUM_H
#define NUM_H
class Num
{
private:
int num;
public:
Num(int n);
int getNum();
};
#endif
and here is my num.cpp:
#include "num.h"
#include <iostream>
#include <stdlib.h>
Num::Num(int x){
num = x;
}
int Num::getNum()
{
return num;
}
all files are in the same directory and whenever I'm using g++ on the command like like this: (g++ (tdm64-1) 5.1.0)
g++ main.cpp num.cpp
there is no problem and everything works just fine
but whenever I'm trying to build it with sublime text it throws me this error
C:\Users\GEBRUI~1\AppData\Local\Temp\ccqq94my.o:main.cpp:(.text+0x1a): undefined reference to `Num::Num(int)'
C:\Users\GEBRUI~1\AppData\Local\Temp\ccqq94my.o:main.cpp:(.text+0x26): undefined reference to `Num::getNum()'
collect2.exe: error: ld returned 1 exit status
[Finished in 0.8s]
I would really appreciate it if someone could tell me what I'm doing wrong here :)
Once you start getting into the realm of needing to compile multiple files, it's really best to start using make or CMake instead of trying to build the compile commands yourself on the command line or in a build system. This way you wouldn't have to edit your build system every time you added a new file to your project.
There is a Make build system that comes with Sublime, but you will need to generate the Makefile externally, either by hand or using tools like autoconf and automake. The CMakeBuilder package looks like it would be useful for working with CMake (I haven't used it myself), although you can of course use external tools as well.

GMock macros not recognized? YCM gives me error, but Bazel builds fine

I am using YCM to provide error checking for my Vim setup. It is throwing errors saying "C++ requires a type specifier for all declarations" at my use of MOCK_METHOD, but Bazel is building just fine. I think I am missing some sort of flag?
This guy has the same problem but did not return to give an answer:
https://github.com/ycm-core/YouCompleteMe/issues/1332
Judging by the person's reply, I knew it was something with my YCM config so I checked that but to no avail.
Here is the simple code
#include <netinet/in.h>
#include <gmock/gmock.h>
#include <string>
#include "socket_helper.h"
namespace Proj {
class MockSocketHelper : public SocketHelper {
public:
MOCK_METHOD(ssize_t, write, (std::string msg), (override));
MOCK_METHOD(std::string, read, (size_t n_bytes), (override));
};
at MOCK_METHOD I get "C++ requires a type specifier for all declarations" which to me tells me it doesn't recognize the macro.
Here are the base flags in my ycmd config:
BASE_FLAGS = [
'-Wall',
'-Wextra',
'-Werror',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-ferror-limit=10000',
'-DNDEBUG',
'-std=c++11',
'-xc++',
'-I/usr/lib/',
'-I/usr/src/googletest/googletest/libgtest.a',
'-I/usr/src/googletest/googletest/libgtest_main.a',
'-isystem /usr/src/googletest/googletest/libgtest.a',
'-isystem /usr/src/googletest/googletest/libgtest_main.a',
'-I/usr/include/'
]
As you can see I tried linking the libgtest.a to no avail. What am I doing wrong?
The -I flag specifies where included files should be located.
Instead of listing the path to the compiled libraries, you should instead set the value of the -I flag to the folder(s) containing the header(s) you're including.
My problem was that in the /usr/include there was a gmock and a gtest folder that was an old version that did not have MOCK_METHOD but MOCK_METHOD0, ... 1, etc. That was the version Clang would give errors against, but my Bazel WORKSPACE file pulled gtest from the Github repo so that's why it compiled correctly.