How to search for header files in usr/include/linux - c++

I am writing a C++ program with cmake in Ubuntu, which uses the header file joystick.h in /usr/include/linux. By default, it does not seem that make can find joystick.h in the default directories if I use #include <joystick.h>. Therefore, I add the line include_directories (/usr/include/linux) to CMakeLists.txt to enable this header to be found.
The problem is, I now get all sorts of errors such as error: redefinition of ‘struct timeval’ and error: redefinition of ‘struct timezone’. This seems to be because there is a header time.h in both /user/include and /usr/include/linux. The header guards in these files are #ifndef _TIME_H and #ifndef _LINUX_TIME_H respectively. Therefore, the only solution I can think of is to hardcode the path to joystick.h in my source code.
Because of this, it seems to me that /usr/include/linux should never be added to the search path due to these redefinition issues. Is this correct? And is my solution of hardcoding the path the best one?

Unless there is a reason not to (and there might be I suppose) it would seem that just using #include <linux/joystick.h> in the source file would be the simplest solution here.

Related

Custom precompiled headers causing compile errors

I am trying to introduce precompiled headers into my project because of long compile times in my project right now. I have a main project that includes a main function and all the real code is in the DLL project(they are linked together.) I named my precompiled header "vpch.h" and i created a vpch.cpp that includes vpch.h. I am using visual studio 2017 so I went into the properties of vpch.cpp and selected Create. Then I added vpch.h as the first thing in all my cpp files. All files are set to use precompiled headers and reference vpch.h Every CPP files throws the error:
Error C1010 unexpected end of file while looking for precompiled header.
Did you forget to add '#include "vpch.h"' to your source?
I am at a loss as to what to do because I have followed many tutorials and can't find a reason for this. As well as most issues that pop up on google are people just accidentally including precompiled headers.
My only thought is that maybe in the section in properties where it asks for the name of the precompiled header, I need to do more than put "vpch.h" and maybe an exact file location? Any help with this is super appreciated.
EDIT
From further debugging it would appear that all but ONE cpp file is throwing an error. The one that doesn't throw an error is the one that exists in the same exact folder as the vpch.h. So files that can't just write #include "vpch.h" and have to write something like "../vpch.h" I can write #include <vpch.h> and I am going to try that now but I am unsure that will help.
The issue was with every CPP file that wasnt in the same folder as the precompiled header.
So if you use a file structure that contains different classes in different folders, using
#include "../../vpch.h" will actually fail. You must add the root folder to your additional include directories and then use #include <vpch.h> for all files. I can NOT tell you why using #include "../../vpch.h" wasn't working. It seems counter intuitive for it to fail in my opinion.
It may be because it searches for the precompiled header in the same folder as the file you are referencing it in. This answer, however, will work as a solution.

include<apis/api1/api.h> throws No such file or directory

#include<apis/api1/api.h>
throws No such file or directory
i even tried moving api.h and api.cc to the main project directory and using
#include<api.h>
does the same thing even though it is in the exact same directory that the other classes use
i tried adding /apis/api1 to the compiler search path
that just crashes the compiler can someone tell me what to type into the compilers compilation line
#include <api.h>
is the way you include a system header. (That is, the header for a library installed on your system.) It does not search in the directory of the source file or in directories specified in -I command line arguments.
#include "api.h"
is the way you include your own headers. (But it will also search library header locations if it doesn't find the header locally.)

Header file not found only in specific translation unit

I'm currently stuck on a compilation problem on Android for my app.
I get the following error during the compilation of my native library with ndk-build:
BackgroundDisplayConfiguration.h:12:23: fatal error: glm/glm.hpp:
No such file or directory
#include <glm/glm.hpp>
^
What puzzles me is that I have specified a path for this header only library in my Android.mk the following way:
LOCAL_CPPFLAGS += -I../../glm/include
and this path exists and is correct, but moreover if I mess up this path I get the same error in other files that include glm.hpp. When the path is correct, only this file yields an error, and I don't understand why. Any pointers?
EDIT: Okay, this is even more puzzling. The include option appear in every compiler command for each file, but not on the compiler command for the big wrapper generated by swig (that outputs my library_native_wrap.o), and that's where it yields an error... Well, it at least explains the observed behavior.
So I found a workaround for this, even though it doesn't feel quite right.
Indeed, I found out that when compiling every source of my library, the compiler command actually had the include option, but then, when compiling the output of swig (that big unique c++ wrapper file), the option wasn't there anymore.
I found a way to correct this by adding my include path to the LOCAL_EXPORT_C_INCLUDES.
For some reason, the LOCAL_CPPFLAGS aren't used when compiling the wrapper...

including secExt.h causes errors

I'm trying to include secExt.h in my C++ project and it gives me the error:
error C2086: 'BOOLEAN SEC_ENTRY' : redefinition
How can I fix it?
EDIT: from microsoft's docs, include security.h, not [secext.h].
original answer (before I googled the header name myself, no info given by OP):
if the header is yours, add
#pragma once
at the top.
if that doesn't work (there is reportedly a compiler on some IBM system or something that doesn't support #pragma once), then use a header guard.
if the header isn't yours, create a header wrapper like this:
#pragma once
#include <secExt.h>
then include your wrapper header instead of including [secExt.h] directly.

Why do I get an including <iostream.h>?

I have been trying to import iostream into a custom block, I added the line
#include <iostream.h>
in the .h file and in the .cc file but I get the error:
fatal error: iostream.h: No such file or directory
The iostream header is <iostream>, not <iostream.h>. The error you're getting suggests that the compiler is looking for iostream.h, which suggests that you might be including the wrong header.
Try changing the header to <iostream> and see if that fixes the problem. More generally, make sure you aren't including any C++ standard library header files suffixed with .h unless they come from C as well (in which case you should probably use the C++ versions of the headers anyway).
Hope this helps!