Compiling Maya Plug-ins with CMake

New year, new site design! I’ve posted some free videos talking about how to compile Maya plug-ins on all platforms using CMake. Check them out here:

22 thoughts on “Compiling Maya Plug-ins with CMake”

Marco Giordano January 25, 2015 at 2:15 am

Amazing work as usual man! keep rocking

Matt January 28, 2015 at 2:24 am

Hi Chad,
I was trying to use your module to compile spReticle plugin under OSX 10.9 for Maya 2015. I found the plugin is building properly, but libraries are not linked based on error given:

ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make[2]: *** [src/spReticle.bundle] Error 1
    make[1]: *** [src/CMakeFiles/spReticle.dir/all] Error 2

Also ld is looking at /Applications/Autodesk/maya2015/Maya.app/Contents/MacOS/Debug which does not exist.

Chad January 28, 2015 at 8:38 am

Hi Matt,
Maya on OSX requires a different compiler (gcc 4.2) than the default clang. The easiest way to get it is with brew. brew install apple-gcc42. Then you can run cmake like this:

cmake -G "Unix Makefiles" -DCMAKE_C_COMPILER:FILEPATH=/usr/local/bin/gcc-4.2 -DCMAKE_CXX_COMPILER:FILEPATH=/usr/local/bin/g++-4.2 ../

PJ April 24, 2015 at 11:51 pm

Hey Chad,

Is there a more elegant way to implement the includes for xgen’s headers and libs? At the moment I’m just adding it to the end of my CMakeList.txt as an extra include. I was curious if there was a way to create a FindXgen.cmake where it requires FindMaya.cmake so it can use it’s ${MAYA_LOCATION} variable? Then just append the directories to the MAYA_INCLUDE_DIR and MAYA_LIB _DIR? I’m new to cmake and I’m having a difficult time finding answers on the web.

set(SOURCE_FILES
    "main.cpp"
    "main.h"
)

find_package(Maya REQUIRED)
find_package(OpenGL REQUIRED)

include_directories(${MAYA_INCLUDE_DIR})
link_directories(${MAYA_LIBRARY_DIR})
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${MAYA_LIBRARIES})

# XGen Specific setup
set(XGEN_PLUGIN_BASE_PATH "${MAYA_LOCATION}/plug-ins/xgen")
set(XGEN_INC_SUFFIX "${XGEN_PLUGIN_BASE_PATH}/include")
set(XGEN_SEEXPR_SUFFIX "${XGEN_INC_SUFFIX}/SeExpr")
set(XGEN_XGEN_SUFFIX "${XGEN_INC_SUFFIX}/xgen")
set(XGEN_LIB_SUFFIX "${XGEN_PLUGIN_BASE_PATH}/lib")

include_directories(${XGEN_INC_SUFFIX} ${XGEN_SEEXPR_SUFFIX} ${XGEN_XGEN_SUFFIX})
link_directories(${XGEN_LIB_SUFFIX})


MAYA_PLUGIN(${PROJECT_NAME})
install(TARGETS ${PROJECT_NAME} ${MAYA_TARGET_TYPE} DESTINATION plug-ins)

PJ April 25, 2015 at 9:45 pm

I think I got a working FindXgen.cmake. I think it works, I’m still having troubles linking though so I’m not exactly sure if I did it right.

# - Maya finder module
if(NOT DEFINED MAYA_LOCATION)
    # Set a default Maya version if not specified
    if(NOT DEFINED MAYA_VERSION)
        set(MAYA_VERSION 2015 CACHE STRING "Maya version")
    endif()

    # OS Specific environment setup
    set(MAYA_INSTALL_BASE_SUFFIX "")
    if(WIN32)
        # Windows
        set(MAYA_INSTALL_BASE_DEFAULT "C:/Program Files/Autodesk")
    elseif(APPLE)
        # Apple
        set(MAYA_INSTALL_BASE_DEFAULT /Applications/Autodesk)
    else()
        # Linux
        set(MAYA_INSTALL_BASE_DEFAULT /usr/autodesk)
        if(MAYA_VERSION LESS 2016)
            SET(MAYA_INSTALL_BASE_SUFFIX -x64)
        endif()
    endif()
    
    set(MAYA_INSTALL_BASE_PATH ${MAYA_INSTALL_BASE_DEFAULT} CACHE STRING
        "Root path containing your maya installations, e.g. /usr/autodesk or /Applications/Autodesk/")
    
    set(MAYA_LOCATION ${MAYA_INSTALL_BASE_PATH}/maya${MAYA_VERSION}${MAYA_INSTALL_BASE_SUFFIX})
endif()


# OS Specific environment setup
set(XGEN_PLUGIN_BASE_PATH "${MAYA_LOCATION}/plug-ins/xgen")
if(WIN32)
    # Windows
    set(XGENLIB libAdskXGen.lib)
elseif(APPLE)
    # Apple
    set(XGENLIB libAdskXGen.dylib)
else()
    # Linux
    set(XGENLIB libAdskXGen.so)
endif()

# XGen library directory
find_path(XGEN_LIBRARY_DIR ${XGENLIB}
    PATHS
        ${XGEN_PLUGIN_BASE_PATH}
    PATH_SUFFIXES
        "lib/"
    DOC "XGen library path"
)

# XGen include directory
find_path(XGEN_DIR XgObject.h
    PATHS
        ${XGEN_PLUGIN_BASE_PATH}/include
    PATH_SUFFIXES
        "xgen/"
    DOC "XGen include path"
)

# SeExpr include directory
find_path(SEEXPR_DIR SeExpression.h
    PATHS
        ${XGEN_PLUGIN_BASE_PATH}/include
    PATH_SUFFIXES
        "SeExpr/"
    DOC "SeExpr include path"
)

set(XGEN_INCLUDE_DIR ${XGEN_PLUGIN_BASE_PATH}/include ${XGEN_DIR} ${SEEXPR_DIR})
#message(STATUS ${XGEN_INCLUDE_DIR})

# Xgen libraries
set(_XGEN_LIBRARIES AdskCaf AdskFabricGeom AdskFabricMath AdskGLee AdskPartio AdskSeExpr AdskSubEngine AdskXGen AdskXgUI AdskXpd)
foreach(XGEN_LIB ${_XGEN_LIBRARIES})
    find_library(XGEN_${XGEN_LIB}_LIBRARY NAMES ${XGEN_LIB} PATHS ${XGEN_LIBRARY_DIR}
        NO_DEFAULT_PATH)
    set(XGEN_LIBRARIES ${XGEN_LIBRARIES} ${XGEN_${XGEN_LIB}_LIBRARY})
endforeach()


include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Xgen DEFAULT_MSG XGEN_INCLUDE_DIR XGEN_LIBRARIES)

PJ April 25, 2015 at 10:47 pm

Looks like the FindXgen.cmake did work. I just had to set these flags to resolve the linking issues I was having. Thanks, your tutorial got me there. I didn’t have to use XCODE!

if(APPLE)
    ## Maya requires gcc 4.2 compiler
    set(CMAKE_C_COMPILER "/usr/bin/gcc")
    set(CMAKE_CXX_COMPILER "/usr/bin/g++")
    set(CMAKE_CXX_FLAGS "-std=c++0x -stdlib=libstdc++")
#    set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libstdc++")
endif()

Chad May 2, 2015 at 11:30 am

@PJ Cool I’m glad it worked. I would have probably just added it all in FindMaya. Also Maya 2015 and on does actually work with XCode 6.1/Clang 6. You just need to do what you put down in your snippet. I did something like this in FindMaya:

if (APPLE AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
    # Clang and Maya needs to use libstdc++
    set(MAYA_CXX_FLAGS "-std=c++0x -stdlib=libstdc++")
endif()

Then in your program CMakeLists, you can say:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MAYA_CXX_FLAGS}")

You just need to be mindful of mixing things like mixing string types and such.

Luiz May 6, 2015 at 4:35 pm

Hi Chad,
Im using Maya 2014 on Windows. I used your FindMaya cmake module and CMakeLists files but when I try building with cmake I get the errors:

No CMAKE_C_COMPILER could be found.
and
No CMAKE_CXX_COMPILER could be found.

Do you think it is something related to cmake installation, or visual studio?

Thank you for putting up all this information. This is great!

Chad May 9, 2015 at 1:44 pm

Hi Luiz. Do you have Visual Studio installed? I think 2014 uses Visual Studio 2010.

David Johnson May 17, 2015 at 6:48 am

Amazingly clear and concise tutorial Chad. Exactly what I needed. Many thanks and much appreciation.

Felix June 13, 2015 at 1:52 pm

Hi there,
and first of all chad thanks for this great instruction, I am really looking foward to improve my build pipline.

Unfortunately i run into issues using Visual C++ 2012 Express Edition. In former times, i.e. with VC 2008/2010 one needed the Windows SDK for 64 bit compilation, but with 2012 that should actually not be necessary anymore.

Has anyone got this working?

best
Felix

Matt November 19, 2015 at 6:46 am

Hi Chad,

back again. Still having problems with some plugins, that I need to compile through GCC. I’m not able to finish configuring cmake build:

CMake Error at /usr/local/Cellar/cmake/3.4.0/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
Could NOT find Maya (missing: MAYA_LIBRARIES)
Call Stack (most recent call first):
/usr/local/Cellar/cmake/3.4.0/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE)
cgcmake/modules/FindMaya.cmake:93 (find_package_handle_standard_args)
src/CMakeLists.txt:14 (find_package)

Chad December 10, 2015 at 9:57 pm

Hi Matt,
Make sure you download the devkit. In more recent versions of Maya, it is a separate download.

Andy Seymour February 18, 2016 at 7:59 am

If you are trying to compile for a recent version of Maya such as 2016 The MAYA_LIBRARY_DIR seems to get confused if you have Maya 2013.5 (The DX11 Extension Version) installed as well. It detects the Maya2013.5 directory instead.

Chad February 23, 2016 at 8:38 pm

That’s strange. I haven’t seen that before, but then again I don’t have Maya 2013.5 installed.

eric June 3, 2016 at 8:54 am

Hi Chad, I’m attempting to use your findMaya cmake file for newer versions of maya. However, no matter what i set as the maya version, or change the findMaya.cmake file to default to a more current version of maya, I still get compiler errors

No rule to make target `/usr/autodesk/maya2016/lib/libOpenMaya.so’

I’ve more or less removed all mentions of 2016 in the findMaya.cmake, so where might this be coming from?

Thanks!

Chad June 15, 2016 at 10:22 pm

Hi Eric, What platform and maya version are you trying to use?

eric June 16, 2016 at 8:11 am

hey Chad, linux 2017 with qt 5.6.1. It seems like a few things have changed in regards to cmake and qt5, and my cmake skills are not up to speed yet 🙂

Thanks!

Pusht October 20, 2016 at 11:26 am

Hi!
I dont see how this is supposed to be correct ? :
find_path(MAYA_INCLUDE_DIR maya/MFn.h
“maya” folder is not situated directly in the install folder….

Stepan March 16, 2017 at 2:51 am

Hi Chad,
great tutorial! Could you suggest a compiler I could use to compile for mac and linux on windows?

Stephan Osterburg May 7, 2017 at 5:28 am

Hello Chad,
I followed your tutorial which was super informative and well explained.

But I am still unable to compile. And was wondering if you had some pointers of what I do wrong here. I actually downloaded at the end your version from github. I am on MacOS Sierra 10.12.4, python 2.7 with cmake 3.7 and trying to compile for Maya 2017.


cmake -G "Unix Makefiles" ../

CMake Error at /Users/stephanosterburg/anaconda/lib/python2.7/site-packages/cmake/data/CMake.app/Contents/share/cmake-3.7/Modules/FindPackageHandleStandardArgs.cmake:138 (message):
Could NOT find Maya (missing: MAYA_INCLUDE_DIR MAYA_LIBRARIES)
Call Stack (most recent call first):
/Users/stephanosterburg/anaconda/lib/python2.7/site-packages/cmake/data/CMake.app/Contents/share/cmake-3.7/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
module/FindMaya.cmake:93 (find_package_handle_standard_args)
CMakeLists.txt:12 (find_package)

Thanks a lot for the help,
Stephan

Chad May 8, 2017 at 8:26 pm

@stepan You can’t compile for mac and linux on Windows. You’ll need either a virtual machine or an actual machine with those operating systems.

@Stephan It can’t find the Maya install. Check to make sure the install location is the same as inside the FindMaya file. If it’s different, you can just update it to point to wherever it’s installed on your system.

T May 23, 2017 at 10:58 am

Your comment is awaiting moderation.

Hello Chad,

This tutorial series has been really useful! Thank you so much for making it!

I have one small issue that I was hoping you might be able to clarify:

I’m building on Mac OS X for Maya 2016 and managed to follow along and modify your FindMaya script to match my needs and configuration (as far as I can tell), and I am able to build successfully. However, I am having an issue getting make to install the plugin in the install directory the way that you did in part 6 of the tutorial series. Whenever I try to build using the “–target Install” option, make gives me the following error: “make: *** No rule to make target `Install’. Stop.”

I’m not super familiar with makefiles, so it would be great to know how I might be able to fix this issue.

Anyway, thank you again for the great tutorial!

Leave A Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

<code> For inline code.
[sourcecode] For multiline code.