C++包含问题

C++ include issue

本文关键字:问题 包含 C++      更新时间:2023-10-16

我正在使用C++为Autodesk Maya编写一个插件,但存在链接器错误。

我的主要课程是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
}

然后我有一个Utilities类,其中包含一些静态方法,标题看起来像

#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__) */

像这样的实现

#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) {
   ...
}

然而,我得到以下错误

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

我认为这是一个链接错误,在某个地方有一些循环引用,但我不知道它在哪里

感谢您的帮助。

谢谢。

我知道这已经一年了。但几分钟前我又偶然发现了。。。

添加

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

到您编写插件代码的头文件或cpp文件。包括

#include <maya/MFnPlugin.h>

直接在初始化插件的main.cpp中。

maya中的大多数示例都有以下字符串:

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

在包括任何内容之前。例如这里。

如果您有多个包含MFnPlugin.h 的文件,则可能会发生此问题