Xcode preprocessor macros to include system style headers - c++

I'm trying to manually add freetype 2.9.0 as a framework build target to my toy project in Xcode. Freetype uses preprocessor macros to include its headers with angled brackets (<freetype/config/xx.h>) which is confusing the Xcode build system and I get the error expected "FILENAME" or <FILENAME>, indicating that it can't find my system level includes. Instead of hardcoding the subfolder headers, how would I configure the Xcode build system to find these config files?
I tried this in another file
#define SOME_CONFIG <glfw/glfw3.h> /*this header is included as a system header in a subfolder <project/glfw/... , and works fine*/
#ifdef SOME_CONFIG
#include SOME_CONFIG
#endif
And it works perfectly. But when I try this with freetype it cannot find the expanded in the macros.
The freetype include structure looks like this:
include/ftbuild.h -- points to include/freetype/config/ftheader.h
include/config/ftheader.h -- has all the #define macros to ->
include/config/... -- a bunch of headers with options which Xcode refuse to see

Apparently it was actually working all along I just forgot to add the environment variable FT2_BUILD_LIBRARY to both debug and release build, and I had it set to debug, so the internal header macros was not being precompiled, and therefore not found. Works perfectly fine to do what I am trying to do in Xcode 9.

Related

error: unknown type name '__darwin_wctype_t' typedef __darwin_wctype_t wctype_t compiling on MacOS with Clang

Completely new project is totally fine, but I found pretty old opensource project and want to compile it. Basically, error appears when including headers like iostream or algorythm.
My environment looks like the following:
MacOS 10.15.4
Clang 11.0.3
Cmake 3.16.5
Tools and headers are installed here /Library/Developer/CommandLineTools/usr/bin
Project uses cotire 1.8.0
IDE - CLion
In general, project consists of 30+ sub-projects some are C some C++. Only those in C can be built.
I've been trying a lot of stuff: setting target version for Cmake, setting paths where to look for headers manually (in Cmake files). Re-installing developer tools does not help as well.
Please, see original error message:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:37:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:214:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iosfwd:95:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/wchar.h:118:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/wchar.h:92:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/_wctype.h:42:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/__wctype.h:62:
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/_types/_wctype_t.h:32:9: error: unknown type name '__darwin_wctype_t'
typedef __darwin_wctype_t wctype_t;
UPDATE 1
I've moved a bit forward, and figured out that stdafx.h cannot even include iostream. So I will check all include paths.
There was an issue with includes. Project has its own _types.h, and there is one in system. Project's variant was included first and system's one was ignored. Obviously, this leads to such cryptic messages.
This is a root cause.Clang's documentation says I need to use -isysroot to explicitly set system's headers root and this will prevent matching project's header first, but it doesn't work for me. Temporarily, I changed project's header name(types.h) and it worked well(without underscore it is no longer considered as a system header), in the meantime I will try to fix it properly.

Including headers in subfolders

I work on a C++ program which I develop on my personal Ubuntu 15.04 machine which eventually has to run on openSUSE 13.1 at work. To make it work on both systems I carefully chose the libraries and the versions to use.
Now I have a problem with the includes. I want to use Qwt 6, which is available on both distributions. The location of the header files differs, though. On Debian they are at /usr/include/qwt/qwt_*.h but on openSUSE they are at /usr/include/qwt6/qwt_*.h. My code currently has #include <qwt/qwt_plot.h>. This does not work on openSUSE since I would have to insert that 6 there.
The easiest solution that I currently see is just including either directory using CMake and then just writing #include <qwt_plot.h> in the source code. However, I think that this is not a really nice solution since those subdirectoryies of /usr/include are there to provide namespaces. Just adding all directories to the include path will mangle those together and might even lead to conflicts.
Is there a nice way to solve this?
In fairness: This is for a project I am paid to work on.
In CMake you can configure platform checks like you could for autoconf. The idea is that you include a config.h file that always exists but use tools to generate that file in each platform. You can check how here but as a summary you can have a config.h.in file with the checks you want to make and use in your headers.
#cmakedefine HAVE_QWT_H
#cmakedefine HAVE_QWT6_H
You then have CMake check if the headers are present and process that file into a final config.h file. In the CMakeLists.txt file you could have the following.
INCLUDE (CheckIncludeFiles)
CHECK_INCLUDE_FILES (qwt/qwt_plot.h HAVE_QWT_H)
CHECK_INCLUDE_FILES (qwt6/qwt_plot.h HAVE_QWT6_H)
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
Finally, in your headers, you include the config.h and use the macros to conditionally include one header or another.
// always included
#include "config.h"
// conditionally include headers based one macros from config.h
#ifdef HAVE_QWT_H
#include <qwt/qwt_plot.h>
#elseif HAVA_QWT6_H
#include <qwt6/qwt_plot.h>
#else
#error QWT headers required and not present in supported locations
#endif

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 ;)

Include <string> not found compile error in Xcode 4.2

I'm getting include not found compile error in XCode. I have an iOS app project that i use Objective-c and c++ as mix.
Initially, i created one .h file and one .cpp file in my ios project. Then, I renamed the .cpp file to .mm file.
Here is my .h file;
TestLog.h
#ifndef CalculatorDemo_TestLog_h
#define CalculatorDemo_TestLog_h
#include <string>
using namespace std;
class TestLog
{
private:
string logString;
public:
void Log(string logMessage);
};
#endif
TestLog.mm
#include "TestLog.h"
void TestLog::Log(string logMessage)
{
//this->logString->append(logMessage);
}
What am I missing? Do I need to add std c++ library to my targetS? Something related to Search Header Paths?
I just need to use string type.
Thanks much for in advance
select project -> build setting -> apple LLVM compiler 5.1 -> language
In Compile Sources As change to Objective-C++
There's a quirk in XCode. I noticed it in 7.3. Most projects recognize .mm files and the STL, while one project I had did not. The fix was that I had to click on the top-left project icon, then click Targets > Build Phases > Link Binary with Libraries > and add in AppKit.framework. Next, I had to click Targets > Build Settings > search on "Compile Sources", and set it to "Objective C++" on all possible columns. Then, do a Clean and then a Build from the Product menu. This compiled properly then. Then, go back to that Compile Sources again and set it back to "According to File Type" on all possible columns. Then, click Build from the Product menu again. At that point, it compiled properly and allowed me to utilize the "according to file type" option, which I like better.
Oh, and if doing Cocoa stuff, don't forget to add the following header in your files:
#import <Cocoa/Cocoa.h>
And if doing command line stuff, don't forget to add the following instead of the Cocoa header:
#import <Foundation/Foundation.h>
i believe you need to include the whole path to the library. similarly to say "foundation" & "uiview" frameworks.
#import <Foundation/Foundation.h>
or
#import <UIKit/UIKit.h>
and yes, make sure you add the library to your target.
So I was having this issue with the Cocoapods library Bypass and none of these solutions did anything. The problem was with a file which Cocoapods creates called an umbrella header. This is located in <POD_NAME>/Support Files/<POD_NAME>-umbrella.h. Delete it, and it should build just fine.
Now for the explanation of why this is necessary: the umbrella header is mixing both C++ and Objective-C code directly in a header which is a big no-no apparently and ends up completely breaking the C++ imports. By removing it (which seems to have no effect?) this conflicting import which Cocoapods unknowingly created will go away.
Ran into this with xcode 12.4 with a project that is objective-c, but where I need one C++ modul. Solution: wrap the contents of the .h file in:
#if defined __cplusplus
declarations
#endif
Apparently xcode is not good at detecting a mix of sources.
see Expected ; after top level declarator, error in xcode
This often happens when Xcode doesn't understand that a C++ header file you've imported into Objective-C is actually for C++ code.
So you can solve this problem by finding the Objective-C file that imports C++ code, and simply change its extension from .m to .mm

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

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.