How to #include files in separate directories using different IDEs? - c++

I am working on a C++ project using Xcode on MacOS X, and am now starting to port it to Linux using the Code::Blocks IDE.
Many of my source files are in separate directories and I am having issues including them.
Here is an example of this issue:
folder1/foo.h
folder2/dog.h
foo.h includes dog.h with: `#include "dog.h"`
It works fine on Xcode if both files in the same project but if I try it in Code::Blocks it has an error finding it.
I can fix this issue in Code::Blocks by changing the code to use a relative include path such as:
#include "../folder2/dog.h"
Unfortunately doing this stops Xcode from being able to find the file.
How can I fix this issue so I can compile the same code in multiple IDEs? I would like to avoid throwing all the source in the same folder. Should I use a preprocessor statement similar to:
#if XCODE
#include "dog.h"
#else
#include "../folder2/dog.h"
#endif

Rearrange your structure so that one project has only one common include directory:
/project/
/src/*.cpp
/include/*.hpp
/folder1/dog.hpp
/folder2/cat.hpp
Now say #include <config.hpp> and #include <folder1/dog.hpp> etc., and add to your compiler flags:
-I ${PROJECT_DIR}/include

How a given compiler/IDE locates dependencies is, unfortunately, entirely compiler/IDE-specific. There is no way to arrange this in such a way that it will be honoured by all development environments.
I don't know Xcode or Codeblocks, but I'm sure there must be some project configuration that controls where they looks for #include files.

Related

How can I use QT creator "follow symbol under cursor" feature without using qmake

I have an existing C++ project that uses a standard makefile which I have imported into QT creator 4.1.0 on Ubuntu 16.04. It compiles and runs fine from the build menu in creator, but in the editor some of the #includes are underlined in yellow (with error "No such file or directory"), and the symbols from those headers consequently fail to resolve when I use the "follow symbol under cursor" feature.
Includes such as the following appear to work fine:
#include <vector>
#include <sys/types.h>
#include "myInclude.h"
Whereas includes from custom libraries do not:
#include <some_namespace/something.h>
If I change the above non-functional include to:
#include "../../../some_library_proj/include/some_namespace/something.h"
then it works.
There is a file myproject.includes that includes the path ../some_library_proj/include/some_namespace, but that doesn't appear to have any effect on the problem at hand.
I have searched for similar discussions online, and they usually mention editing a .pro file, however I believe this should not apply in my case since I'm not using qmake.
What files or environment variables do I need to change to get my imports working?
Include the lower-level folders in the myproject.includes file as well. In this case:
../some_library_proj/include/

Freetype invalid preprocessor directive Eclipse

I've downloaded the latest version of FreeType and want to get the source code running in my program. I'm programming in Eclipse and I've copied all the Freetype files into my project. I've listed them under ProjectName/Source/FreeType2/..
I've added compiler include directories for the new folders, so my GCC C++ compiler knows where to look for them. However, if I build my project, an error occurs on the last line of the following code:
#include <ft2build.h>
#include FT_WINFONTS_H
#include FT_INTERNAL_DEBUG_H
I did some research and the macro file FT_INTERNAL_DEBUG_H is defined as <internal/ftdebug.h>. The file is present in my system and the macro file FT_WINFONTS_H compiles like a charm! I think it's got something to do with my directory stucture somehow. How should I change my directory structure in order to get things compiled succesfully? My current structure is like this:
ProjectName
Source
FreeType2
devel
docs
include
config
internal
objs
src
I know I used two "source" folders, but this shouldn't be the problem, right?
The error message I get is Invalid preprocessor directive: #include FT_INTERNAL_DEBUG_H
Thank you for your time ;)

vs2010 - Can not open include file 'sys/param.h

When i compile my C++ solution in vs2010 x64 mode, i get the below compilation issue.
Can not open include file 'sys/param.h' :No such file or directory.
But the same compiles fine in Win32 mode.
I am not sure how this header file is missing.Can any one help me on this?
I am using some of the client headers and this is the below code section that is present in the client file.
#ifndef WIN32
#include <sysipc.h>
#include <sys/param.h>
#endif
The include #include <sysipc.h> should be #include <sys/ipc.h>, however, this is a POSIX header file that is meant for Linux build projects so it won't work for any Visual Studio projects. Since you're compiling for x64, WIN32 flags might not be set by default.
Try changing the macro to:
#ifndef _MSC_VER
#include <sys/ipc.h>
#include <sys/param.h>
#endif // !_MSC_VER
Hope that helps.
This is highly likely a consequence of some #if going wrong - e.g. it's checking for _M_IX86, and it not being set on a 64-bit system, it picks up something non-windows and tries to compile that.
sys/param.h is a unix/linux header-file, and you shouldn't expect to find that in your Windows system. [edit: unless you hooked in a version of the GNU compiler or did some other modification to the compilation tools core of your MSVC build environment]
Unfortunately, without seeing the source code, all we can possibly do is explain the possible reasons...

Xcode Error - No such file or directory

I'm trying to build a project of mine that includes fuzzylite C++ libraries within a Carbon C++ application. However the compiler throws out an error for each fuzzylite's library I include in my source code. I've tried to include the Header Search Path and the Library Search Path on my target application build info but it doesn't work.
I've included the header file using double quote markers just like the following example:
#include "fuzzylite/test.h"
How can I include such library in my project and get it to work properly?
Easy, you need clean the path: #include "fuzzylite/test.h" for ALL #include, like this: #include "test.h"
From version 3.1, you should use #include fl/Headers.h.
If you are running into problems, I strongly encourage you to report the problem in the forums at http://www.fuzzylite.com, where I and others will be very happy to help you.

Handling stdafx.h in cross-platform code

I have a Visual Studio C++ based program that uses pre-compiled headers (stdafx.h). Now we are porting the application to Linux using gcc 4.x.
The question is how to handle pre-compiled header in both environments.
I've googled but can not come to a conclusion.
Obviously I want leave stdafx.h in Visual Studio since the code base is pretty big and pre-compiled headers boost compilation time.
But the question is what to do in Linux. This is what I found:
Leave the stdafx.h as is. gcc compiles code considerable faster than VC++ (or it is just my Linux machine is stronger ... :) ), so I maybe happy with this option.
Use approach from here - make stdafx.h look like (set USE_PRECOMPILED_HEADER for VS only):
#ifdef USE_PRECOMPILED_HEADER
... my stuff
#endif
Use the approach from here - compile VC++ with /FI to implicitly include stdafx.h in each cpp file. Therefore in VS your code can be switched easily to be compiled without pre-compiled headers and no code will have to be changed.
I personally dislike dependencies and the mess stdafx.h is pushing a big code base towards. Therefore the option is appealing to me - on Linux you don't have stdafx.h, while still being able to turn on pre-compiled headers on VS by /FI only.
On Linux compile stdafx.h only as a precompiled header (mimic Visual Studio)
Your opinion? Are there other approaches to treat the issue?
You're best off using precompiled headers still for fastest compilation.
You can use precompiled headers in gcc as well. See here.
The compiled precompiled header will have an extension appended as .gch instead of .pch.
So for example if you precompile stdafx.h you will have a precompiled header that will be automatically searched for called stdafx.h.gch anytime you include stdafx.h
Example:
stdafx.h:
#include <string>
#include <stdio.h>
a.cpp:
#include "stdafx.h"
int main(int argc, char**argv)
{
std::string s = "Hi";
return 0;
}
Then compile as:
> g++ -c stdafx.h -o stdafx.h.gch
> g++ a.cpp
> ./a.out
Your compilation will work even if you remove stdafx.h after step 1.
I used option 3 last time I needed to do this same thing. My project was pretty small but this worked wonderfully.
I'd either go for option 4 or option 2. I've experimented with precompiled headers on both various VS versions and GCC on Linux (blog posts about this here and here). In my experience, VS is a lot more sensitive to the length of the include paths, number of directories in the include path and the number of include files than G++ is. When I measured build times properly arranged precompiled headers would make a massive difference to the compile time under VS whereas G++ was pretty much unimpressed by this.
Actually, based on the above what I did the last time I worked on a project where this was necessary to rein in the compile time was to precompile the equivalent of stdafx.h under Windows where it made sense and simply used it as a regular file under Linux.
Very simple solution.
Add a dummy file entry for "stdafx.h" in Linux environment.
I would only use option 1 in a big team of developers.
Options 2, 3, and 4 will often halt the productivity of other members of your team, so you can save a few minutes a day in compile time.
Here's why:
Let's assume that half of your developers use VS and half use gcc.
Every now and then some VS developer will forget to include a header in a .cpp file.
He won't notice, because the stdafx.h implicitly includes it. So, he pushes his changes in the version control, and then a few other members of the gcc team will get compiler errors.
So, for every 5 minutes-a-day you gain by using precompiled headers, 5 other people waste by fixing your missing headers.
If you don't share the same code across all of your compilers, you will run into problems like that every day. If you force your VS developers to check for compilation on gcc before pushing changes, then you will throw away all your productivity gains from using precompiled headers.
Option 4 sounds appealing, but what if you want to use another compiler at some point in time ? Option 4 only works if you only use VS and gcc.
Notice that option 1 may make gcc compilation suffer a few seconds. Although it may not be noticeable.
It's simple, really:
Project->Project Settings (Alt + F7)
Project-Settings-Dialog:
C++ -> Category: Precompiled Headers -> Precompiled Headers radio buttons --> disable
Since stdafx.h is by default all the Windows-specific stuff, I've put an empty stdafx.h on my other platform. That way your source code stays identical, while effectively disabling stdafx on Linux without having to remove all the #include "stdafx.h" lines from your code.
If you are using CMake in your project, then there are modules which automate it for you, very convenient, for example see cmake-precompiled-header here. To use it just include the module and call:
include( cmake-precompiled-header/PrecompiledHeader.cmake )
add_precompiled_header( ${target} ${header} FORCEINCLUDE SOURCE_CXX ${source} )
Another module called Cotire creates the header file to be precompiled (no need to manually write StdAfx.h) and speeds up builds in other ways - see here.
I've done both option 2 (#ifdef) and option 4 (PCH for gcc) for cross platform code with no issues.
I find gcc compiles much faster than VS so the precompiled headers are generally not that important, unless you are referencing some huge header file.
I have a situation where #2 in particular didn't work for me (There are numerous VS build configs where a #ifdef around #include "stdafx.h" does not work). Other solutions were suboptimal because the files themselves were cross-project as well as being cross-platform. I did not want to force preprocessor macros to be set or force linux or even windows builds to use (or not use) pch, so...
What I did, given a file named notificationEngine.cpp, for example, was removed the #include stdafx.h line entirely, created a new file in the same directory called pchNotificationEngine.cpp with the following contents:
#include "stdafx.h"
#include "notificationEngine.cpp"
Any given project can just include the correct version of the file. This admittedly is probably not the best option for cpp files that are only used by a single project.