Clang cannot find include directory? - c++

I just moved all my header files to be in the include directory of my project, rather than having both .cpp and .h in the src directory. I'm using YouCompleteMe in vim and now when I open vim, it tells me that my header files cannot be found on the side to alert me of compiler errors. Specifically, it gives me errors on the #include "my_header.h" of my_header.h file not found and errors on each method saying use of undeclared identifier. However, the code compiles just fine. And the auto-completion works fine. It seems that clang cannot find where my header files are located now since I moved them ../include. If I change my #include line to be #include "../include/my_header", all of the errors to away. But it seems better to only have #include "my_header.h" in all of my .cpp files or #include <my_header.h>.
Can anyone help me solve this issue?

Related

In WSL, VS Code intellisense can't resolve c++ lib includes in .h header files

I can't figure out how to fix this annoying intellisense problem. Everything compiles just fine though. If i change the extension to .hpp everything is ok, but with .h it just can't find any c++ standart libraries.
All i have is test.h file with #include <iostream> in it. . I've added "/usr/include", "/usr/local/include" to both include and browse path but it didn't change anything.
Error:
#include errors detected. Consider updating your compile_commands.json or includePath. Squiggles are disabled for this translation unit (/mnt/c/dev/opengl_cmake/src/shader/test.h).C/C++(1696) cannot open source file "iostream"C/C++(1696)

file not found error when trying to include id3lib header

I downloaded id3lib and placed the directory in my main.cpp directory but both g++ and visual studio give file/directory not founds and "undefined" errors
Here is my main.cpp:
#include <iostream>
#include <id3lib-3.8.3/include/id3/tag.h>
int main() { std::cout << "hi"; }
g++ main.cpp gives:
main.cpp:2:46: fatal error: id3lib-3.8.3/include/id3/tag.h: No such file or
directory
#include <id3lib-3.8.3/include/id3/tag.h>
if I use "" instead of <>, i get this error:
id3lib-3.8.3/include/id3/tag.h:32:30: fatal error: id3/id3lib_frame.h: No
such file or directory
#include <id3/id3lib_frame.h>
It's not enough to put it beside your main file. As you can see in your first approach when you used #include with <> it can't find it, that's because (copied from here) :
For #include <filename> the preprocessor searches in an implementation
dependent manner, normally in search directories pre-designated by the
compiler/IDE. This method is normally used to include standard library
header files.
You didn't tell your compiler where to look for id3lib-3.8.3/include/id3/tag.h so <> will not work for you.
Then you tried "". it found id3lib-3.8.3/include/id3/tag.h but in the tag.h there is #include <id3/id3lib_frame.h>, So back to problem with first approach, right?
What you need to do is that you need to tell your compiler/IDE where to look for these files. In visual studio click right on your project file, then properties->C/C++->General->Additional Include Directories and add the include library ($(ProjectDir)id3lib-3.8.3/include/ or maybe $(SolutionDir)id3lib-3.8.3/include/) to it. Then your first approach should work fine.

Header files not found even though they are in the right directory?

I'm fairly new to programming using linux so forgive me for any dumb errors I might make in my question but basically I am trying to compile using the terminal (C++) and my code in a .txt file however I keep getting a fatal error that my header file can't be found? When I try to type
g++ -o test main.cpp header.h
I get the error stating "header.h: no such file or directory" in the terminal. I've ensured that both the cpp and header files are in the same directory but no luck there. I've also used
#include <"header.h">
in my main.cpp and header file to try different fixes. I've researched and looked at different answers but no fixes either. Any suggestions?
#include <"header.h">
Use either
#include <header.h>
will lookup the standard include directories for these header files first
or
#include "header.h"
will lookup all include directory pathes specified with the preprocessor options
but don't mix these up.
Also you don't need to specify the header in the compiler command line
g++ -o test main.cpp header.h
# ^^^^^^^^ omit this
That's what the #include statement in your code is for.

Why does Visual Studio error when linking having files included?

I don't really understand the why behind this, but I think I've figured out the what.
My brother is working on a homework project and they're using Visual Studio. He was having issues getting his project to compile. The project in question consisted of:
main.cpp
classes.h
class_functions.cpp
general_functions.cpp
They had the following import structure:
main.cpp
#include "classes.h"
#include "class_functions.cpp"
#include "general_functions.cpp"
class_functions.cpp
#include "classes.h"
general_functions.cpp
#include "classes.h"
He kept getting link errors when trying to compile, even though g++ would compile it just fine. So I tried via Visual Studio command prompt and cl. This compiled just fine, so I tried msbuild which (obviously?) failed. I went into the vxproj file, because hey, what is it telling cl that I'm not?
In the proj file it was including classes.h, general_functions.cpp and class_functions.cpp, as well as main.cpp. When I removed general and class functions, msbuild ran just fine. Put either of them back and boom. classes.h contains
#ifndef myclasses
#define myclasses
/* Code here */
#endif
And the other .cpp files contain similar directives.
I finally figured out how to eliminate the problem - eliminate
#include "general_functions.cpp"
#include "class_functions.cpp"
from the code base, and now Visual Studio compiles this just fine.
The question is why? What is the justification or possible benefits from doing this?
the difference between .cpp and .h files is that the .h files are usually included with the #include directive while .cpp files are included in the linking process.
The #include instruction will insert a copy of the included file in the code you are compiling, so by including the .cpp files you will compile a single source file that contains all your .cpp files. It's very bad practice but still could work.
But if you are then adding the .cpp files also in the linking step then you are adding those files a second time creating probably duplicate definitions of your functions, e.g. linker error. I think that's probably the main error (without the linker error codes I'm just guessing).

#include <string> file not found in iOS in C++ file

I have a objective c/c++ project under iOS, moving it from OS/X and I get a 'file not found' error on
#include <string>
It's a clean project, and I've just added the files from the old project. Are the STL includes set up in XCode? A find produces a number of possibilities e.g.
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/usr/include/c++/4.2.1/debug/
but adding this to the search path just threw up more errors. Any suggestions?
(apart from don't use string - it's in house code I'm porting)
xcode 4.2.1, ios5.0 running on OS/X 10.7.3 and it's in a .cpp file, the code works fine on OS/X
Are you really sure <string> is included only from a .cpp file?
I just tested on a fresh project, by adding a .cpp file and including <string>, and it works, just as expected (same Xcode version, same SDK version).
But if I include <string> in a .m file, then of course I got a «file not found» compiler error.
So double-check this, as the error may come from here.
Do you include <string> from a .cpp file only, or from a .h file, intended to be used by a .cpp implementation?
Then maybe it's also included by a .m file, hence the error.
Also check your precompiled headers, if any, to see if you include some C++ stuff there...
Also remember, in that later case, that you can rely on the __cplusplus macro, when needed.
If you include a header in an ObjC file and it includes <string> then you hit errors like this. For all .m files XCode uses a C compiler (clang or llvm-gcc). For all .mm files it will use (clang++ or llvm-g++).
I suggest going through and renaming all your .m files to .mm. Including main.m to main.mm.
For me the reason was
MyHeader.h (which includes #include ) target was public. Changed it to project and it compiled.
For cocoa pod:
s.public_header_files = 'MyProject/Classes/**/*.h'
s.project_header_files = 'MyProject/Classes/MyHeader.h'