Author Topic: Unable to use Outpost2 TYPEDEF Export in custom class  (Read 2542 times)

Offline Vagabond

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1013
Unable to use Outpost2 TYPEDEF Export in custom class
« on: November 26, 2016, 09:54:17 AM »
I was happily coding a custom class for Outpost 2 and I wanted a couple of the class member functions to be passable to Outpost 2 triggers. The problem is I'm using #include <vector>, so when I add the Export TYPEDEF to a member function, I get the following error: linkage specification is not allowed.

I googled the problem and found what appears to be a nice writeup by a gentleman named Michael Burr: http://stackoverflow.com/questions/20139642/c2732-linkage-specification-error

Following his instructions, I enclosed the header file like this:

Code: [Select]
#if __cplusplus
extern "C" {
#endif

#include "Outpost2DLL\Outpost2DLL.h"
#include <cmath>
#include <vector>

class DisasterHelper
{
public:
    Export void DisasterHelper::CreateRandomDisaster();
   
    ... MORE BAD CODE BY VAGABOND...

};
#if __cplusplus
}
#endif
ERRORS
Then when compiling the source code, I get multiple of the following two errors:

  • For Vector and associated headers: this declaration may not have extern 'C' linkage
  • For cmath: 'abs': second C linkage of overloaded function not allowed

RESULTS/QUESTIONS
1.  Does this mean if I want to include C++ only classes in a custom Outpost 2 class, the class cannot contain the TYPEDEF Export?

2.  The issue with cmath might be caused by an issue with my project settings, not sure. I'm getting the following caution when building a working copy of the scenario: LNK4098 defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library, which I have not really investigated yet.

I could fall back to c style arrays and just manage the array sizes manually. I would rather not though (call me lazy if you want), so just looking for feedback if the only options for a custom class are either to not use VECTOR or to not use Export.

Offline Arklon

  • Administrator
  • Hero Member
  • *****
  • Posts: 1267
Re: Unable to use Outpost2 TYPEDEF Export in custom class
« Reply #1 on: November 26, 2016, 06:28:37 PM »
Things using C++ features, meaning STL stuff or functions inside classes, can't have extern C linkage. You could technically use C++-style exports with triggers, but you have to use their mangled name, which isn't very straightforward. Also, the Export macro expands to 'extern "C" __declspec(dllexport)'.

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4954
Re: Unable to use Outpost2 TYPEDEF Export in custom class
« Reply #2 on: December 01, 2016, 04:39:30 AM »
Keep in mind a class member function needs to be passed the hidden "this" pointer. The trigger code in Outpost 2 won't know the source object to pass. It only has a function address.

Code: [Select]
// C++
object.method();
// Rough equivalent in C
object_method(&object);

The trigger callback won't know what to pass for the hidden parameter.