MOC adding namespace to class names - c++

I have this very strange problem while compiling the project.
MOC seems to be adding a namespace to the class name being moc'ed, although it's not mentioned anywhere in the file/class.
The namespace, however, exists in a library which I use, but it's hidden far away in the header files and I don't use it in the UI files. This is what MOC generates:
const QMetaObject SmpTl::CaptureController::staticMetaObject = {
{ &QObject::staticMetaObject, qt_meta_stringdata_SmpTl__CaptureController,
qt_meta_data_SmpTl__CaptureController, 0 }};
The SmpTl namespace is not mentioned anywhere in the declaration of CaptureController, but it appears in the MOC-generated .cpp file.
I'm using Visual Studio with the QT integration.

I also ran into this problem. I had code that looked like this:
namespace foo {
#ifdef _WIN32
... // This code was fine
#else
#error Not Supported
#endif
}
This confused MOC into thinking namespace foo never closed. Apparently, it didn't know _WIN32 was defined, and got tripped up by the fact that I forgot to put quotes around the error message. Changing it to:
#error "Not Supported"
fixed my problem.

SmpTl is the namespace CaptureController is defined in, as it was found by MOC.
The Q_OBJECT macro expands into the declaration of the staticMetaObject-variable inside your class definition (among other things it expands into). The MOC-file contains the definition of that variable.
If this is not correct, please post your Qt version and a stripped down version of your header-file.

Related

Bug in Xcode 7 about precompiled header and forward declaration in C++

I found a bug in latest Xcode 7.0 that annoys us very much in our company because it makes most of our C++ code not debuggable. After lots of experiment, I was able to reproduce it with a minimum amount of code.
In some cases, it is impossible to see members inside a C++ class on LLDB. It seems that three conditions must be present for that bug to appear:
the class is forward declared
the class has a virtual method
the class is declared inside a precompiled header
I am asking whether somebody else already knowns about that bug and what is the recommended procedure to report that bug (to LLVM or Apple ?).
Steps to reproduce:
Create two source files with their content:
header.h
#ifndef HEADER_INCLUDED
#define HEADER_INCLUDED
class A; // forward declaration, has an effect on bug
class A
{
public :
virtual ~A() {}
protected:
int doYouSeeMe;
};
#endif
PCHAndFDbug.cpp
#include "header.h"
int main()
{
A* a = new A();
return 0;
}
Create a small Xcode 7 project with these two files. header.h must be set as a precompiled header (Prefix header setting in Xcode). As a reference, I am using Premake to generate that project and here is the premake5.lua source:
solution "PCHAndFDbug"
configurations {"Debug"}
xcodebuildsettings { MACOSX_DEPLOYMENT_TARGET = "10.7" }
project "WithPCH"
language "C++"
files {"PCHAndFDbug.cpp", "header.h"}
kind "ConsoleApp"
pchheader "header.h"
project "WithoutPCH"
language "C++"
files {"PCHAndFDbug.cpp", "header.h"}
kind "ConsoleApp"
Place a breakpoint on the return 0 statement. Check if you can see member doYouSeeMe in a variable.
Same issue for me. Fixed by turning off "Enable Clang Module Debugging" in the Build Settings
For Apple at least, you should submit a bug report through the developer centre https://developer.apple.com/bug-reporting/
I've encountered this bug too and it is annoying.

C++ Inclusion guards being "ignored" between projects

I have two projects; a static library and an executable (standard Win32 setup, using the multithreaded debug dll setting for both).
In my libray, I have a Globals.h, with the following code:
#ifndef _GLOBALS_H
#define _GLOBALS_H
#include <SDL.h>
#include <string>
namespace Eng
{
bool Run = true;
SDL_Window *Window = NULL;
SDL_GLContext GlContext;
Uint32 WindowFlags = SDL_WINDOW_OPENGL;
}
#endif
This is all fine and dandy, and within that project everything works fine. However, as soon as I include that file within more than one file in my executable project (all protected by inclusion guards), I start getting multiply defined symbol errors for each variable within Globals.h.
As well, I'm getting quite a few macro redefinition warnings from math.h (M_PI macro redefinition). I'm not sure if this is linked, but it seems likely due to the similar nature of error (guards defined in one project seemingly not applying in the other).
Anyone have any ideas how to resolve this? I feel like I'm missing some vitally important compiler setting somewhere :(

boost.future causing problems with clr

I am using visual studio 2012 with a clr library that needs to link to a native library.
My library which is using boost::future.
I am having this problem when I use future::then(F &&) function against the managed project:
Error 910 error LNK2022: metadata operation failed (8013119F) : A TypeRef exists which should, but does not, have a corresponding TypeDef: (dummy): (0x0100003e). D:\ClrProject\somefile.obj
I tried, as suggested in other questions, to make the dummy types in the library complete, since I cannot forward declare a nested struct from inside a template, as can be done with boost::thread::dummy struct.
This did not solve the problem.
My setup is the following:
Boost 1.55.
Using boost .dlls.
define BOOST_RESULT_OF_USE_DECLTYPE
define BOOST_THREAD_PROVIDES_FUTURE
define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
I am using these very defines on the dependent .dll as well, to make sure that all parts of the API are exposed correctly.
Solved.
#Hans Passant your suggestion was right. I just wasn't putting the #pragma the correct way.
I had to put the pragmas so that my headers are compiled as unmanaged code. Once that was done, it seems that template instantiations for my code were emitted as unmanaged. At that point, problems disappeared.
So what I did is something like this:
#pragma managed(push, off)
#include "MyHeaderWithFutures.h"
#pragma managed(pop)
void f() {
//
f = myObject.Something().then(...);
}

Is it possible to undefine a class in C++ (Visual Studio)?

I know, it may seem strange, but my goal is to undefine a class in C++. The root of the problem is in combining TinyXML2 and Boost unit tests.
Contents of the header file (Configuration.h) which is being tested:
...
#include <tinyxml2.h>
...
And this is the contents of my configurationTests.h file:
#include "unitTests.h"
#include "Configuration.h"
BOOST_AUTO_TEST_SUITE(configuration_test)
BOOST_AUTO_TEST_CASE(basic) {
...
}
BOOST_AUTO_TEST_SUITE_END( )
When I try to compile my tests, I'm getting an error:
error C2371: 'XMLDocument' : redefinition; different basic
types c:\program files (x86)\windows kits\8.0\include\um\msxml.h 10085
Inside this file (msxml.h) on line 10085 we have this class definition:
class DECLSPEC_UUID("CFC399AF-D876-11d0-9C10-00C04FC99C8E")
XMLDocument;
When I remove those two lines, my tests do compile and everything seems fine. Of course, this is not a solution, but that fact prooves that something inside Boost unit tests library includes msxml.h and somehow leads to conflict with TinyXML2 library.
I tried different solutions found in Google (like writing "#define WIN32_LEAN_AND_MEAN"), removing "using namespace tinyxml2" and making changes inside tinyxml2.cpp - nothing actually helps.
So, my question is simple: can I undefine (unload?) previously defined class in compile time in some tricky way? I tried "#undef XMLDocument", "#define XMLDocument 1" + "#undef XMLDocument" - nothing works.
Update: Actually, I kinda solved the problem by writing "#define MSXML_LIBRARY_DEFINED" on the first line of configurationTests.h. But still, I would love to know an answer to this topic question.
I think you have used default namespace for tinyxml2.
Try to delete using namespace tinyxml2 and then use it like in this example:
tinyxml2::XMLDocument xDoc;
try this:
namespace your_name_space
{
#include <tinyxml2.h>
}
From now all classes inside the tinyxml2 are hidden by your namespace.
The namespece must be declared also for the tinyxml2.cpp file.

c++: definition of dllimport function not allowed, building with visual studio 2010

I'm using visual studio 2010 to build a .dll. I wrote up a trial as:
// trialDLL.h
#ifndef TRIALDLL_H_
#define TRIALDLL_H_
// ... MyMathFuncs class definition omitted
#ifdef __cplusplus
extern "C"{
#endif
#ifdef TRIALDLL_EXPORT
#define TRIALDLL_API __declspec(dllexport)
#else
#define TRIALDLL_API __declspec(dllimport)
#endif
TRIALDLL_API MyMathFuncs* __stdcall new_MyMathFuncs(double offset);
TRIALDLL_API void __stdcall del_MyMathFuncs(MyMathFuncs *myMath);
TRIALDLL_API double __stdcall MyAdd(MyMathFuncs* myMath, double a, double b);
// some other similar stuff
#ifdef __cplusplus
}
#endif
#endif
And the triallDLL.cpp file:
// trialDLL.cpp
#include "trialDLL.h"
TRIALDLL_API MyMathFuncs* __stdcall new_MyMathFuncs(double offset)
{
return new MyMathFuncs(offset);
}
TRIALDLL_API void __stdcall del_MyMathFuncs(MyMathFuncs *myMath)
{
delete myMath;
}
TRIALDLL_API double __stdcall MyAdd(MyMathFuncs *myMath, double a, double b)
{
return myMath->Add(a, b);
}
// ... some other definitions
With these two files in the project, I added a property sheet to the project through visual studio 2010 property manager and added TRIALDLL_EXPORT to user macros. After all these, the nice Intellisense gives me error for each function defined in the .cpp file and complains "error: a function declared 'dllimport' may not be defined". So it appears that Intellisense doesn't find TRIALDLL_EXPORT defined. I thought it might make a difference if I actually build the project, but the result suggests the same error: "error C2491: 'new_MyMathFuncs' : definition of dllimport function not allowed". Then it is clear that the macro TRIALDLL_EXPORT is still not defined in compiling time.
After failing to add macro through visual studio, I also tried putting code line: #define TRIALDLL_EXPORT in trialDLL.cpp but it didn't help either. I wonder what's the proper way to do this? How do I inform the compiler that the micro is defined so that TRIALDLL_API evaluates to dllexport rather than dllimport?
Also, if I can build the .dll successfully, is there any systematic way to test/verify the functionality of the .dll?
Thanks for any help in advance! (Although I know it's an issue here on stackoverflow to put appreciation in the question, I feel myself impolite not to do so. Forgive me for any inefficiency caused by these lines.)
"User macros" in VS property sheets have nothing to do with preprocessor macros. Put TRIALDLL_EXPORT into the property sheet's section C/C++ > Preprocessor > Preprocessor Definitions
"User macros," which can only be defined in property sheets, allow you to create your own "variables" usable in Visual Studio properties, similar to the built-in ones $(TargetName), $(SolutionDir) etc.
As said in this Microsoft article, you cannot apply the __declspec(dllimport) keyword to implement a function. You should use it only in the declaration. For example:
// function declaration
void __declspec(dllimport) funcB();
// function definition
void funcB() {
//funcB code
}
Put
#error Where is my macro?
in the #else block of the header. Then experiment with the project settings or the #define until you get it right. Did you perhaps only add the property sheet to one configuration? Did you put the #define at the very top of the file? Do you have any PCH stuff that causes it to ignore your settings? And so on.
The code looks okay, and must work if TRIALDLL_EXPORT is actually defined. You most probably messed up that somehow (like set it for a different configuration or for just one file) or did not rebuild.
If you're completely lost, ask for the preprocessor output, and look at that. As with define there can not be dllimport around at all, the error is also impossible.
EDIT: I just noticed you wrote _I also tried putting code line: #define TRIALDLL_EXPORT in_ trialDLL.cpp. I thought you put it at top of the header for trial. Try that first just to see that it works fine. Then you can remove it after you found the proper place.