Eclipse C++: Project builds but does not run - c++

I included a custom header file, with a "test" function defined in it, as I am learning to include custom header files.
I was able to add the directory of the header file to the compiler "include" list, and Eclipse built the project fine.
However, when I try to run it, I get an error saying that it can't find the header file anymore.

use include like this:
#include "../HeaderFolder/header.h"

Related

How do I fix unexpected end of file error with pch.h

So I have been trying to learn cpp and I was writing a program, and when I try to build the solution, it gives an error saying
unexpected end of file while looking for precompiled header. Did you forget to add #include "pch.h" to your source?
Then I included it and I got the same error, and also another saying
cannot open source file pch.h
One option, if you are new to c++, is to just turn off pre-compiled headers in the project settings.
It needs to be the first include, you can't place it under other includes.
Your .cpp file is probably not in the same directory as pch.h
Try adding the directory that your pch.h is in to the additional includes, even if it is at the root of your project.
quick solution to a frustrating issue when trying to add .pch to an exisiting project:
if you have a /include /src dir structure, it might not work,
unless you place the "pch.h" and "pch.cpp" in the same dir /src.
Also: mark the "MyPreComp.cpp" as /Yc - create,
and in the .cpp files you want to use the .pch set them to Yu - use.
#include "pch.h" as the first #include in the .cpp
NB. You need to set "not using precompiled headers" to all .cpp files not using them,
yes, it IS a hassle.
( Visual Studio 2019 )
It needs to be included to each cpp file (by default)
It needs to be included in the very first line of your code (excluding the comments, it's ok to have the fancy comments on top)
It needs to be in a reachable directory. This error often happen when you have a folder structure in your project. So this can happen with a source files in some nested folder, when your precompile-header-file is up there in main. In this case, either add necessary number of "../" before the file name, or add the main folder to the "additional include directories" as it is already suggested above.
It needs to actually be the same precompile header file, that is set as the one in project setting. Check the file with "Precompiled Header" option set to "Create (/Yc)", ensure that it refers to he same header file, that you include ("pch.h" or "stdafx.h" by default) This error often happens when you include some old source to newer proj, or vice-versa, due to different default names in different studio versions: "stdafx.h" vs "pch.h".
If all above is set up, and you still have it, check if you actually set it up for the right build configuration. Always apply project setting change for all configurations. Costed me some nerves when I did it for only one config, and was trying to compile another:

How to make header files can be include via Library name?

I'm trying to make a cross-platforms crypto library in C++ at https://github.com/haithngn/cryptor something like https://github.com/MailCore/mailcore2
The Structure is:
Can I make any header files can be include in the statements like this:
#include <Cryptor/MD5Encryptor.h>
I can include these header directly from absolutely file path
../core/CryptorCore.h
But these format will make the source code cannot build succeed in an XCode Project.
I tried to simulate the MailCore2 but that's very difficult.
Hope you can suggest me any solution or do a favor PR on my Repository.
Thanks,
You need to have a proper hierarchy. First, no, you can't have
#include <Cryptor/MD5Encryptor.h>
with your current setup, not while building the library, and not without flattening the hierarchy when installing your files (which CMake can do).
What you can do is:
#include <Cryptor/core/abstract/MD5Encryptor.h>
if you add your project inside a Cryptor folder instead of being at the root of your project.
I would advise you to create a Cryptor.cmake file that would allow people to pick up your library once installed, so that they don't have to know where the library includes are or what the libraries name is.
It should not be necessary to point to every single header file. Just add all directories containing these header files with include_directories(PATH_TO_HEADERS).
For the include of the header file src/core/CryptorCore.h with
#include "CryptorCore.h"
you have to point to
include_directories(${PROJECT_DIR}/core/)

Cant compile rocksdb, dependencies not found

I am trying to compile a program that uses rocksdb.
According to the example in the official webpage, the only header i should add to my code is db.h.
Now, the file i am compiling is in folder A.
db.h however is in A/rocksdb-master/include/rocksdb/.
So, i add this line to my file:
#include "rocksdb-master/include/rocksdb"
It finds the file, but the problem is that inside db.h, i have this line:
#include "rocksdb/metadata.h"
And when i compile i get this error:
fatal error: rocksdb/metadata.h: No such file or directory
#include "rocksdb/metadata.h"
I mean, it's obvious. db.h is in the same folder as metadata.h, so it's fine that the compiler cant find any rocksdb folder. But i doubt that people who wrote this library don't know that.
Is there any other way to add the path's to compile it?
Why is it that the path from db.h are not relative to where it is located?
You should normally use just the following header in your project:
#include "rocksdb/db.h"
When compiling your own project, you should then add the RocksDB include path to the list of include directories. For example, if the RocksDB source code is in directory ../rocksdb-master, the include path will be ../rocksdb-master/include.
How to add the include path to the compiler flags is indeed compiler-specific. With g++ or clang, it's done by passing -I../rocksdb-master/include to the compiler when compiling your own program. Note that you many need to link against the RocksDB library as well.
And finally, you may need to include some more RocksDB headers if you use some of its advanced concepts, e.g. transactions.

Xcode - header files issue with .cpp file

I have imported my project from windows to mac and Is using xcode to compile the project.
I have managed to fix most error and bugs from the porting process but there is one bug/error I am unable to determine why its not working as it should. This probably due to my lack of knowledge of xcode but was wondering if anyone is able to help me with this problem.
The problem is this;
I have a two sub folders in my project, once called include and another called source. Of course include contains all the header files and source contains all the cpp files and such.
Example:
Root/
Include/
Header.h
Source/
test.cpp
Now in the build settings in the xcode project, I have added $(SRCROOT)/Include/ in the Header Serach Paths. So therefore I presume that xcode will include any header files in the Include folder.
So in the test.cpp class I add #include "Header.h". When I click build I get an error saying "'Header.h' file not found".
Im not entirely sure what I am doing wrong, of course I can do this for example - #include "../Include/Header.h" but that's going to be a pain going through all the cpp files in my project (which is a lot).
I was just wondering if anyone else came across this problem?
Thanks for reading.
In general you need to add the headers to the User Header Search Paths, which if you search the help in XCode will give you the format you need to pass the directory as.

Question about C++ folder structure for header file

I'm quite new in C++ after few years in Java and eclipse, I got little bit confusing using code::blocks, no autogenerate setter/getter and also implement interface :D.
I wanna ask about code structure in code::blocks, I create new console application, my header will be put to Headers/include folder called Employee.h, then .cpp will be put to src folder.
Now I create main class (I put outside of src folder) who will call the header, I just append the include code like this :
#include "Employee.h"
sure then the errors appeared after compiling:
error : Employee.h: No such file or directory.
how to link the header to the main class properly?
this is my folder structure :
updated :
It works, my include folder needs to be added on build options.
Really thanks
You need to add your include directory to your compiler's include path. This is going to be compiler-specific. e.g., if your structure is:
code
code/src
code/include
and you're running g++ from a terminal in the 'code' directory, you'd need to run (assuming your .cpp is Employee.cpp):
g++ -Iinclude src/Employee.cpp
I suspect you're running some sort of IDE, though: if so, do a search in its help for "include path" and it should tell you how to set it up correctly.
If you want to include your employee.h you must #include "employee.h" not Employee.h. Those are two different files.
You shouldn't be adding include paths to your build options for header files that are actually part of your project. It didn't find the header file from the EmployeeTest.cpp because you didn't use the full relative path.
You need:
#include "include/Employee.h"
You should only be adding include paths to your compiler for additional libraries that aren't added to the typical /usr/local/include or /usr/include directories.