How to compile the header file with Q_OBJECT macro in Xcode? - xcode

I create C++ project in Xcode which links against the Qt framework. The hello world program works well. When I add a class derived from QObject and add the Q_OBJCET macro, there is link error.
The class is
MyObject.h
#ifndef MyObject_h
#define MyObject_h
#include <QtCore/QObject>
class MyOBject : public QObject
{
Q_OBJECT
public:
MyOBject();
};
#endif
MyObject.cpp
#include "MyObject.h"
MyOBject::MyOBject()
{
}
I know I should use the moc to compile the MyObject.h first and add the generated moc_MyObject.cpp to the Xcode project.
In Microsoft Visual Studio, I can configure this header file to be compiled with moc custom tool. And add the generate cpp file to VS project.
But this is Xcode. My question is: Is there equivalent mean in Xcode to compile the header file including Q_OBJECT macro?

Here's how you do it in Xcode 6:
Select your target and in Build Rules make a new custom rule.
Set the "Process" drop down to "Source files with names matching" and type in *.h next to that.
Set the "Using" drop down to "Custom script:" and in the script type something close to this:
/path/to/your/Qt/bin/moc ${INPUT_FILE_PATH} -o ${DERIVED_FILE_DIR}/${INPUT_FILE_BASE}_moc.cpp
You'll have to change the path to wherever your moc executable is.
Now set the "Output files" to:
${DERIVED_FILE_DIR}/${INPUT_FILE_BASE}_moc.cpp
That's it for the Custom Rule.
Now to get the headers to be compiled with this rule. While your target is still selected go to "Build Phases". Expand the "Compile Sources" section, hit the "+" button at the bottom. Find and add the header files with Q_OBJECT classes in them. Add those headers to your project first if you can't find them in there.
That's it, Xcode will then run moc on those headers and it will understand that the output from moc is a .cpp and needs to be compiled and linked into you app and do that for you.

I didn't find how to set the custom tool for a specific header file in Xcode. I found a workaround via the build phase script (Build Phases -> Add Build Phase -> Add Run Script). I added the moc command line as script, and included the generated moc_MyObject.cpp file to MyObject.cpp. It works now.

Related

QT : "this file is not part of any project"

I created a project folder (test1), then when creating the C++ source file main.cpp, it says that this file is not part of any project although i created it in the same project test1 by doing :
File -> new file or project -> C++ -> C++ source file.
Open the .pro file and check if something like this is missing:
SOURCES += main.cpp
(with proper path if needed)
Also it seems you did not configure the project. In "projects" you should choose a kit to use when building. This may also be a cause of the warning.
For a .creator project (without Qmake) deleting the .creator.user file while Qt Creator is closed helped in my case.

Generating a custom ItemGroup in .vxcproj using CMake

I'm trying to use CMake to add an entire directory structure to my Visual Studio project. I know it is possible in VS because I already succeed by manually copying the root folder of the structure in the same directory of the project and by selecting "Include in Project". However, I'm not sure that it would be possible with CMake.
My .vcxproj contains this ItemGroup after this manual operation:
<ItemGroup>
<Text Include="asd\test.txt" />
<Text Include="asd\asd2\test.txt" />
</ItemGroup>
As you can see I just added two folders and two txt files. This produces the result I'm trying to achieve.
Any idea on how to generate this with CMake?
CMake is meant to hide the details of the build/OS environment from the user. This requires some generalization across platforms/environments. So it's difficult to get an exact copy of a manual made .vcxproj project.
You can add any files to CMake targets - explicitly including none-source files - that will then be added by CMake to generated IDE projects.
If I understand your particular case correctly the problem is to get Text instead of None tool name in ItemGroup source file's listing.
Since CMake version 3.7 you can use VS_TOOL_OVERRIDE source files property to override CMake's default for non-source files.
The following small example:
cmake_minimum_required(VERSION 3.7)
project(HelloWorld)
file(WRITE main.cpp [=[
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
}
]=])
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/asd/test.txt" "")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/asd/asd2/test.txt" "")
add_executable(HelloWorld "main.cpp" "asd/test.txt" "asd/asd2/test.txt")
set_source_files_properties(
"asd/test.txt"
"asd/asd2/test.txt"
PROPERTIES VS_TOOL_OVERRIDE "Text"
)
Does give your expected results. CMake just generates absolute paths by default.
Reference
C++ CMake (add non-built files)

Use external header files in MPLAB X IDE

I have a folder with some .h and .c files and I want to use header files in my projects.
I have included them in "Header Files" folder of my project using "Add Existing Item" but when i try to "#include" them compiler(mplabc18\v3.41) say "unable to locate file xyz.h"
So, what should i do to use these files without copying them into the project folder?
Just add the header to the project using the "add" dialog and select "this file is for this project, use relative path" dont remember if it is exac this text but its something like.
After that just do the normal declaration in your file:
#include "your_header.h"
This should work fine.
--UPDATE
To work with the new MPLAB X
Do the follow:
Click on the File-> Project Properties
Select the Conf -> C18 (Global Options) -> mcc18
For XC8, this is under Conf > XC8 compiler
Click on the "..." button of the propertie "Include directories"
Click on "Browse Button"
Locate you project directory
Click on Open, then Ok and Apply
Build your app !
Now it should work.
I know this is an old question, but wanted to add another tip since I just stumbled across it myself. If you go back and forth between Windows and Linux systems, be sure to pay attention to the capitalization in the filename. On Windows, it doesn't matter. However, on Linux, you need to be sure your #include reference has the same capitalization as the actual file.
If the file is saved on disk as 'UARTIO.INC', your include needs to be:
#INCLUDE "UARTIO.INC" **EXACTLY**
If you put it as:
#INCLUDE "UARTIO.inc", or #INCLUDE "uartio.inc"
It will work fine on Windows, but will fail with "Could Not Find Include File" errors on Linux.
Note that setting MPLAB to ignore case sensitivity doesn't matter for this.

yacc and bison in visual studio

I am going to port the C project that was for unix into windows. So far, I could make it compile but not build . The problem I am getting is , some of the functions which are declared in the header files are defined in the yacc files.so I am getting the following errors:
error LNK2001: unresolved external symbol function_name
I am adding .y and .l files in the source directory of the project.I think I could not port yacc files into windows version or am I doing something stupid.I search it on web but could not get proper tutorial for it.Could you please let me know
How could I add .y or .l files in the project.
How would I make those file compatible to the windows?
How can I link them with other object files.
EDIT
I tried with changing the the .l files into the .yy.c files using the flex.exe.Following is the command for it
c:\> flex.exe name.l
Supposing that both the flex.exe and name.l are in C;>.And I loaded those all those files .l .y(previously present for parsing in unix system) .yy.c(corrsonding yacc file for windows) in the solution of previously exisiting project. Once I compile,I get the following
Can't read the header file parserheaderfile.h
This is the header file which needs to be generated by the bison in the unix
system. So I think I am not able to make the bison compatible for windows .So please him me how can I solve this problem?
Thanks in advance.
You need to add custom build rules for your .y and .l files. To do this, you need to
create one dummy .c file for the .l file and the .y file
add the .l, .y and both .c files to the project
right click on the .l file and select properties
Configuration->All Configurations
General->Item Type->Custom Build Tool
Apply
Custom Build Tool->General->Command Line->flex -olexer.c lexer.l
Custom Build Tool->General->Outputs->lexer.c
Custom Build Tool->General->Additional Dependencies->parser.y parser.c
Apply
select the .y file in the solution explorer
Configuration->All Configurations
General->Item Type->Custom Build Tool
Apply
Custom Build Tool->General->Command Line->bison -oparser.c parser.y
Custom Build Tool->General->Outputs->parser.c parser.h
Also you need to have flex.exe, bison.exe and m4.exe in the system search path. Another drawback is that VS does not get the dependencies right, so when you change something in the parser or lexer files, you need to manually rebuild the project.

Including headers from custom framework in XCode

Am I supposed to adjust FRAMEWORK_SEARCH_PATHS or HEADER_SEARCH_PATHS when I add custom frameworks to the project?
I have MainProject.xcodeproject that links SomeFramework.framework that's simply dragged from "Products" in SomeFramework.xcodeproject to "Link with Binary Libraries" build phase in main project.
Framework contains all required headers in its Headers directory. However, in my project I can't simply use:
#import <SomeFramework.h> // I'm pretty sure this file exists
to include this header. Build fails "No such file or directory". Compiler flags include -F…/SomeFramework/build/Release and that directory contains framework with Headers directory symlink in it.
(BTW: this is for Mac OS X. I don't care about iPhone.)
Just adding the path to the directory containing the framework to FRAMEWORK_SEARCH_PATHS will work. Unless it's a typo, your problem seems to be
#import <SomeFramework.h>
which should be
#import <SomeFramework/SomeFramework.h>

Resources