4

I am writing a plugin for Autodesk Maya using C++ and have a linker error.

My main class is Maya_Search_Plugin.cpp

#include <Utilities.h>

DeclareSimpleCommand( search_face, PLUGIN_COMPANY, "4.5");

//doIt method is entry point for plugin
MStatus search_face::doIt( const MArgList& )
{
    //calls to Maya types/functions and Utilities functions
}

Then I have a Utilities class containing some static methods with header looking like this

#ifndef __Maya_CPP_Plugin__Utilities__
#define __Maya_CPP_Plugin__Utilities__
//#pragma once

//standard libs
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <iostream>
#include <math.h>

//maya libs
#include <maya/MDagPath.h>
#include <maya/MFn.h>
#include <maya/MFileIO.h>
#include <maya/MIOStream.h>
#include <maya/MFnMesh.h>
#include <maya/MFnTransform.h>
#include <maya/MGlobal.h>
#include <maya/MSelectionList.h>
#include <maya/MSimple.h>
#include <maya/MTypes.h>
#include <maya/MPointArray.h>
#include <maya/MObjectArray.h>


class Utilities{
    public: static const int max_mov = 50;
public:
    static double get_mesh_error(MPointArray, MPointArray, int);
    static MStatus translateManipulator(double amount, MObject *path);
    static void GetSelected(MObjectArray* objects, MFn::Type type);
};

#endif /* defined(__Maya_CPP_Plugin__Utilities__) */

with the implementation like this

#include <Utilities.h>

double Utilities::get_mesh_error(MPointArray a, MPointArray b, int vertexCount){
   ...
}


MStatus Utilities::translateManipulator(double amount, MObject *path){
   ...
}


void Utilities::GetSelected(MObjectArray* objects, MFn::Type type) {
   ...
}

However I am getting the following error

duplicate symbol _MApiVersion in:
    /Users/tmg06qyu/Library/Developer/Xcode/DerivedData/Maya_CPP_Plugin-hjrwvybwlvqyyscbmixdkcpdzjqr/Build/Intermediates/Maya_CPP_Plugin.build/Debug/Maya_CPP_Plugin.build/Objects-normal/x86_64/Maya_Search_Plugin.o
    /Users/tmg06qyu/Library/Developer/Xcode/DerivedData/Maya_CPP_Plugin-hjrwvybwlvqyyscbmixdkcpdzjqr/Build/Intermediates/Maya_CPP_Plugin.build/Debug/Maya_CPP_Plugin.build/Objects-normal/x86_64/Utilities.o
ld: 1 duplicate symbol for architecture x86_64
Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld failed with exit code 1

Which I presume is a linking error and there is some circular reference somewhere, but I can't work out where it is.

Any help appreciated.

Thanks.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
shaw2thefloor
  • 600
  • 5
  • 20
  • Maya version? XCode version? – Korchkidu Jul 11 '13 at 15:17
  • Maya 2014, Xcode 4.6.3 – shaw2thefloor Jul 11 '13 at 15:19
  • yes, I'm using the macro in the main class as shown in the demo material in the devkit – shaw2thefloor Jul 11 '13 at 15:21
  • Are you able to rebuild and use plugins samples from the SDK? – Korchkidu Jul 11 '13 at 15:21
  • DeclareSimpleCommand( search_face, PLUGIN_COMPANY, "4.5"); – shaw2thefloor Jul 11 '13 at 15:22
  • Without knowing the technology myself, looks like the symbol '_MApiVersion' is somehow declared in two of your implementation files, 'Maya_Search_Plugin.cpp' and 'Utilities.cpp'. Look at how/why. You might have unknowingly done this using a macro in both, or maybe using a macro in a header file included in both..? :-\ – Grimm The Opiner Jul 11 '13 at 15:23
  • Yes. This was all working before, but I was defining a lot of the same Maya types in both the Maya_Search_Plugin.cpp file and the Utilities.h which seemed wrong. So I put all the includes into the Utilities.h and just included that in both implementations. – shaw2thefloor Jul 11 '13 at 15:24
  • Ah. See my comment above! – Grimm The Opiner Jul 11 '13 at 15:24
  • So I rebuilt the Utilities files. In Utilities.h I used only includes which are used in the method prototypes. In Utilities.cpp I used only includes which were used in in the prototype implementations, and which were not included in Utilities.h. Therefore I have included things in Maya_Search_Plugin.cpp which are also included in both Utilities.h and Utilities.cpp. So there is code duplication for the includes. Seems wrong to me, but it finally compiles again. – shaw2thefloor Jul 11 '13 at 16:13
  • A good rule of thumb is to #include in header files as little as possible. Not never, but as little as possible, even if it means more or 'duplicated' #includes in implementation (cpp) files. – Grimm The Opiner Jul 12 '13 at 13:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33370/discussion-between-shaw2thefloor-and-user1158692) – shaw2thefloor Jul 12 '13 at 14:44

2 Answers2

5

I know this is a year old. But I stumbled on this again a couple minutes ago...

Add

#define MNoVersionString
#define MNoPluginEntry
#include <maya/MFnPlugin.h>

to your header or cpp files where you wrote your plugin code. Include

#include <maya/MFnPlugin.h>

directly in your main.cpp that initializes the plugin.

Most of the examples in maya have the following string:

// This is added to prevent multiple definitions of the MApiVersion string.
#define _MApiVersion

before including anything. For example here.

Pascal
  • 831
  • 2
  • 11
  • 24
0

The issue may happen if you have multiple files which include MFnPlugin.h

Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89