Global Pointer "already defined in" error - c++

I'm writing a little DirectX model viewer.
I just finished writing my scene manger.
I will only ever have 1 scene manger object so my idea was to create a global pointer of type "scnManger" and then set it when i create my scene manger object.
This would let me get to it from any where in my application.
I'm getting a compile error though:
1>shaderViewer.obj : error LNK2005: "class scnManger * sceneManger" (?sceneManger##3PAVscnManger##A) already defined in sceneManager.obj
1>C:\Users\Greg\Documents\Visual Studio 2010\Projects\ShaderViewer\Debug\ShaderViewer.exe : fatal error LNK1169: one or more multiply defined symbols found
right now I have 3 files
sceneManger.h:
// Our global scene manger variable
scnManger* sceneManger;
shadherViewer.cpp (winMain & includes sceneManger.h):
scnManger shaderViewerScnManger;
sceneManger = &shaderViewerScnManger;
sceneManger.cpp (includes sceneManger.h):
I use the methods of my scene manger object in here for various things.
First I would like to understand why I am getting the error, and am also open to any suggestions about a better way to handle this. I wasn't sure if using a global variable like this was a good idea or not.

You should not define global variables in .h files. You should declare them in .h, like this:
extern scnManger* sceneManger;
and then define them in one cpp file, like this:
scnManger* sceneManger;
Otherwise, every cpp file that includes your .h file will declare the sceneManger variable, resulting in a name collision.

Related

Call a method or function from Objective-c in AppleScript

I'm trying to use LestMove to be more precise
the second implementation method where it says:
Option 2:
Copy the following files into your project:
PFMoveApplication.h
PFMoveApplication.m
If your project has ARC enabled, you'll want to disable ARC on the above files. You can do so by adding -fno-objc-arc compiler flag to your PFMoveApplication.m source file. See How can I disable ARC for a single file in a project?
If your application is localized, also copy the 'MoveApplication.string' files into your project.
Link your application against Security.framework.
In your app delegate's "-[applicationWillFinishLaunching:]" method, call the PFMoveToApplicationsFolderIfNecessary function at the very top.
but I'm not able to call the method / Class, could someone help me with this issue? Thanks in advance!
In general, there are a couple of ways to set up an Objective-C class in your AppleScriptObjC project:
Add the file(s) to the project - the Objective-C class name will be
the one used in the #interface/#implementation declarations
Add an outlet property in the AppleScript class/script you are using, e.g. property someProperty : missing value
Instantiate the class programmatically:
set someProperty to current application's ClassName's alloc's init()
or
Connect stuff up with the Interface Builder:
Add an NSObject (blue cube) from the library to your project
Set the class of the object/cube to the class name of the Objective-C file(s) in the Identity Inspector
Connect the AppDelegate IB Outlet to the object/cube in the Connections Inspector
After setting up the outlet property, the Objective-C methods can be used like any other script/class:
someProperty's handler()
That LetsMove project wasn't really set up for AppleScriptObjC, but I was able to tweak it a bit to get it running. I'm not that great at writing Objective-C, but the following worked for me using a new default AppleScript project with Xcode 10 in Mojave (the original file is over 500 lines long, so I'm just highlighting the changes):
Add PFMoveApplication.h and PFMoveApplication.m files to the project (the class name is LetsMove)
Add Security.framework to Link Binary With Libraries in Build Phases
As described in the original project README, add the compiler flag -fno-objc-arc to the Objective-C file in Compile Sources of the Build Phases
-- Now to alter the Objective-C files a bit:
Move the #interface declaration to the .h file and include the redefined method signatures below in it:
The PFMoveToApplicationsFolderIfNecessary and PFMoveIsInProgress methods are redefined as instance methods:
- (void)PFMoveToApplicationsFolderIfNecessary;
- (BOOL)PFMoveIsInProgress;
Redefine the above method signatures in the .m file, and include those methods in the #implementation section - to do this, move the #end to just before the helper methods (after the PFMoveIsInProgress method)
Remove the isMainThread statement at the beginning of the PFMoveToApplicationsFolderIfNecessary method - this is not not needed (AppleScript normally runs on the main thread), and fixes another issue
There is still a little stuff in there from the original app such as NSUserDefaults, so for your own project, give it a look to see if anything else needs changing (dialog text, etc)
And finally, in the AppDelegate.applescipt file, the following was added to applicationWillFinishLaunching:
current application's LetsMove's alloc's init()'s PFMoveToApplicationsFolderIfNecessary()

Firemonkey: How to iterate through all forms in the application using TScreen.Forms

I am attempting to iterate through the forms I have open in my application. I have found the documentation for FMX.Forms.TScreen.Forms, which looks like it can be used to accomplish my goal. However, I am confused on how I am supposed to use it.
At first I tried this in a function within my form's CPP file:
ShowMessage( Forms::TScreen::FormCount );
This produced the error 'Member TScreen::FormCount cannot be used without an object'
I figured that to mean that I need to attempt to access this property from my form, or from the global Application variable. I tried both
this->Forms...
Application->Forms...
and
this->TScreen...
Application->TScreen...
However, neither Forms nor TScreen exist within either of these objects.
How do I go about accessing Forms.TScreen.Forms?
The error gives you a clue:
Member TScreen::FormCount cannot be used without an object
TScreen is a class, not an object. FormCount is not a static member of the class, so you need an object instance of the TScreen class. And such an object is provided for you - the global Screen object:
ShowMessage( Screen->FormCount );
This is stated in the documentation:
FMX.Forms.TScreen
There is a global variable, Screen, of type TScreen, which is instantiated for use by any application with a GUI. Use Screen to obtain information about the current state of the screen in an application.
FMX.Forms.Screen
extern DELPHI_PACKAGE TScreen* Screen;
Here's what works well:
ShowMessage(Screen->FormCount);
Screen is a global object, like Application. The compiler said that FormCount is not static method or smth.

Code compiles until CreateWnd() is thrown in

I am using XE3 and trying to construct a DLL with my third party component. Since it is a rather large project I will describe it then detail the question at hand.
I have multiple cpp files and multiple header files(classes in header files, functions in cpp files). I have everything linking and compiling fine UNTIL I put a CreateWnd() function into one of my classes
void __fastcall TICSByteEntry::CreateWnd(void)
{
TCustomControl::CreateWnd();
SetWindowLong(Handle, GWL_STYLE, GetWindowLong(Handle, GWL_STYLE) | WS_CLIPSIBLINGS);
}
Now it will compile with this code in it but when I put my component on a form and try to run THAT project it will give me an error
'[ilink32 error] Error: Unresolved external '__fastcall TICSByteEntry::CreateWnd() referenced from 'path'unit1.obj'
No other linking issues just that one and as soon as I comment it out everything works nicely as expected. When I was researching this online someone said it is having problems finding the entry point http://edn.embarcadero.com/article/27343. I tried what was recommended and no luck. Any one want to take a guess on what is wrong?
Instead of overriding CreateWnd() you should override CreateParams(). That way the window is created with the style you want and does not need to be changed after creation.

Include Alien in portable app, C++

Ok, I've searched quite a bit, but seem unable to find an answer or example for how to achieve this.
Basically, I have an app that is designed to be portable (built using VC++ in VS2010, but no MFC or managed components, raw WinAPI). I have Lua 5.2 built into it and allow the user to write scripts inside the application. I have multiple glued functions which are exposed to the Lua scripts which handle various WinAPI calls.
However, what I'd like to be able to do is allow the user to write a script in which looks something like this:
require[[Alien/alien]]
local mb = alien.User32.MessageBoxA
mb:types{ 'long', 'long', 'string', 'string', 'long' }
print(mb(0, "Hello World!", "Test", 64))
I simply cannot seem to find a way to do this. I do not want to require the user to install Lua for Windows and, ideally, there be no core.dll and struct.dll from alien; when I tried to do something with those DLLs in ./alien/, it was crashing in Lua5.1.dll because I had LuaForWindows installed, I uninstalled LFW, and then it just states that Lua5.1.dll is missing. I have Lua 5.2 built into my app, so obviously the core/struct DLLs from the Alien rock are expecting Lua5.1.dll to be in the path.
I made a worthless attempt to try to including the Alien src into the project, but doesn't seem to work that way either.
Any ideas would be greatly appreciated. I'd prefer it all be contained in my app, but I'll settle for a solution which involves including the libraries in my project to build and bundle in the distribution if that's the only alternative.
Thanks!
UPDATE:
Ok, thank you Ben Voigt! I think I'm almost there. I've pulled in core.c and struct.c and made sure all the paths are there for libffi. Everything compiles without issue, until I try to call luaopen_alien_core in core.c (the alien.core src file), claiming the identifier is undeclared. I've tried to declare the function signature in my separate source file that's trying to make the call, the compile gets further, but fails complaining of an unresolved external.
Clearly this is likely now a general C++ issue (as I'm only a novice in this area). Here's the general idea of what I have:
//core.c (from Alien project)
(...)
int luaopen_alien_core(lua_State *L) {
alien_register_library_meta(L);
alien_register_callback_meta(L);
alien_register_function_meta(L);
alien_register_buffer_meta(L);
lua_getglobal(L, "alien");
if(lua_isnil(L, -1)) {
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setglobal(L, "alien");
}
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setfield(L, -3, "core");
alien_register_main(L);
return 1;
}
//mysource.c (the file attempting to call luaopen_alien_core(L))
void initLua()
{
L = luaL_newstate();
luaL_openlibs(L);
luaopen_alien_core(L);
(...)
}
This fails to start compiling, issuing the error:
error C3861: 'luaopen_alien_core': identifier not found
Which makes sense, so I add the following line to myheader.h:
int luaopen_alien_core(lua_State *L);
This compiles, but fails to link with:
error LNK2001: unresolved external symbol "int __cdecl luaopen_alien_core(struct lua_State *)" (?luaopen_alien_core##YAHPEAUlua_State###Z)
I've tried several things I can think of, with my limited experience, but nothing will satisfy this error. I even tried to move the contents of core.c into mysource.c, but that creates a whole different mess and seemed to be the wrong way to go as it is.
I'm hoping, and imagining, this is something really stupid, but I'm just not sure how to get it to call luaopen_alien_core, which seems to be the final piece I need.
Thanks again!
}
I imagine that the require directive both loads a dynamic library and adds its contents to the active Lua engine.
By linking alien directly into your code, you obviate the need for the dynamic library. But the content enumeration code won't have run, and you can't use require to run it, or else it'll go looking for a DLL file (along with all the DLL dependencies).
So, you should find out what functions that require directive calls after loading the DLL, and call those when creating a Lua engine. Then it will neither be necessary nor allowed for the script to start with require [[Alien/alien]], but Alien objects will be available.

Passing enums to functions in C++

I have a header file with all the enums listed (#ifndef #define #endif construct has been used to avoid multiple inclusion of the file) that I use in multiple cpp files in my application.One of the enums in the files is
enum StatusSubsystem {ENABLED,INCORRECT_FRAME,INVALID_DATA,DISABLED};
There are functions in the application delcared as
ShowStatus(const StatusSubsystem&);
Earlier in the application when I made calls to the above function like
ShowStatus(INCORRECT_FRAME);
my application used to compile perfectly. But after some code was added The compilation halts giving the following error:
File.cpp:71: error: invalid conversion from `int' to `StatusSubsystem'
File.cpp:71: error: initializing argument 1 of `void Class::ShowStatus(const StatusSubsystem&)
I checked the code for any conflicting enums in the new code and it looked fine.
My Question is what is wrong with the function call that compiler shows as erroneous?
For your reference the function definition is:
void Class::ShowStatus(const StatusSubsystem& eStatus)
{
QPalette palette;
mStatus=eStatus;//store current Communication status of system
if(eStatus==DISABLED)
{
//select red color for label, if it is to be shown disabled
palette.setColor(QPalette::Window,QColor(Qt::red));
mLabel->setText("SYSTEM");
}
else if(eStatus==ENABLED)
{
//select green color for label,if it is to be shown enabled
palette.setColor(QPalette::Window,QColor(Qt::green));
mLabel->setText("SYSTEM");
}
else if(eStatus==INCORRECT_FRAME)
{
//select yellow color for label,to show that it is sending incorrect frames
palette.setColor(QPalette::Window,QColor(Qt::yellow));
mLabel->setText("SYSTEM(I)");
}
//Set the color on the Label
mLabel->setPalette(palette);
}
A strange side effect of this situation is it compiles when I cast all the calls to ShowStatus() as
ShowStatus((StatusSubsystem)INCORRECT_FRAME);
Though this removes any compilation error, but a strange thing happens. Though I make call to INCORRECT_FRAME above but in function definition it matches with ENABLED. How on earth is that possible? Its like while passing INCORRECT_FRAME by reference, it magically converts to ENABLED, which should be impossible. This is driving me nuts.
Can you find any flaw in what I am doing? or is it something else?
The application is made using C++,Qt-4.2.1 on RHEL4.
Thanks.
You should take the enum by value, rather than by const reference. It's small enough to fit into an int, so there is no performance penalty or anything like it.
But, from what you're describing, it sounds like somebody has #defined INCORRECT_FRAME to 0 elsewhere. You should put something like the following in the line above it:
#ifdef INCORRECT_FRAME
#error Whoops, INCORRECT_FRAME already defined!
#endif
BTW, the #ifndef thingy (for your header files) is called an include guard. :-)