A compile conflict between OptiX and MFC

Hi all,
I’m a beginner of OptiX,recently I got a mission to merge the “optixTutorial” sample to an MFC application, which means use the MFC window to display the optixTutorial scene.
Firstly I modified the optixTutorial.cpp, split it into a optixTutorial.h and a optixTutorial.cpp, then, in my MFC application’s VIEW cpp file, I included optixTutorial.h just to use those functions to generate the scene.
But, during the merge, I ran into a compile conflict. To compile the optixTutorial.cpp I have to use C++ preprocessor definition like NOMINMAX,however, after the coding work, I started to compile, my Visual Studio showed many errors like C2059, C2143, C2447, each of them were connected with the min and max methods in optixu_math_namespace.h. Seems like the preprocessor definition didn’t work, I wanna know why and how to fix that, can anybody help me?
And the OptiX version I use is 6.0 and Visual Studio 2015.
Sorry about my poor language expression, I hope you can understand!
Thanks!
Zhang.

try it in this order:

#define NOMINMAX
#include <Windows.h>
#include <math.h>
using namespace std;

#include <optixu/optixpp_namespace.h>
#include <optixu/optixu_math_stream_namespace.h>
#include <optixu/optixu_vector_types.h>
#include <cuda.h>
      
#include <optixu/optixu_math_namespace.h>

#include <cufft.h>
#include <cuda_runtime.h>

if that does not help, (in non-OptiX related files) try to add in those files after all includes:

#undef min
#undef max

// in the non-OptiX files optionally add, if your source code requests min/max:
#define min(a,b)  a < b ? a : b
#define max(a,b)  a > b ? a : b

m1,thanks very much, your method worked but my problem still exist.
Here is what I did:

#include "stdafx.h"    //precompiled header, MFC must include it
// SHARED_HANDLERS 
// ATL 
#ifndef SHARED_HANDLERS
#include "MFC_OPTIXXX.h"  //MFC application header
#endif

#define NOMINMAX    //preprocessor definition,

#include "MFC_OPTIXXXDoc.h"    //MFC document header
#include "MFC_OPTIXXXView.h"   //MFC view header

#include "optixTutrial/tutorial.h"    //optix tutorial header
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

those errors still there, I’m confused…

error code is like:

error C2143: syntax error:missing';'before'{',
error C2062: type 'unsigned int' unexpected

when I check those errors, double click them, they all refers to code in optixu_math_namespace.h, functions like:

/** min */
OPTIXU_INLINE RT_HOSTDEVICE longlong4 min(const longlong4& a, const longlong4& b)
{
    return make_longlong4(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w));
}

I don’t know why, I can compile and run those samples separately with glut. But as long as I combine any of its sample with MFC, those errors will appear. Really confused…

I just created a new default MFC app (in VS 2017 Community using toolset v140 of VS2015) and with CUDA 10.0 on Win10PRO 64bit v1607,
updating project settings:

  • 64 bit (NOTE: OptiX 6.0.0 is 64bit only)
    - Multi-Byte Strings instead of UNICODE UPDATE: Works also for UNICODE
  • switch OFF pre-compiled header
  • target platform 10.0.14393.0; I also tested : 10.0.16299.0 (both work on my system)
  • additional command line options (in C/C++ tab):
    /D_DEBUG -I “$(CUDA_PATH_V10_0)\include”
    -I “C:\ProgramData\NVIDIA Corporation\OptiX SDK 6.0.0\include”
    -I “C:\ProgramData\NVIDIA Corporation\OptiX SDK 6.0.0\include\optixu”

and I updated ClassView.cpp in this way:

#include "stdafx.h"

#include "MainFrm.h"
#include "ClassView.h"
#include "Resource.h"
#include "MFCApplication1.h"

#define NOMINMAX
#include <Windows.h>
#include <math.h>
using namespace std;

#undef min
#undef max
  
#include <optixu/optixpp_namespace.h>
#include <optixu/optixu_math_stream_namespace.h>  
#include <optixu/optixu_math_namespace.h>

using namespace optix;

Context      context;

    #pragma comment( lib, "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0/lib/x64/cufft.lib" )
    #pragma comment( lib, "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0/lib/x64/cudart_static.lib" )
    #pragma comment( lib, "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0/lib/x64/nvrtc.lib" )

#pragma comment( lib, "C:/ProgramData/NVIDIA Corporation/OptiX SDK 6.0.0/lib64/optix.6.0.0.lib")
  #pragma comment( lib, "C:/ProgramData/NVIDIA Corporation/OptiX SDK 6.0.0/lib64/optixu.6.0.0.lib")

void createContext()
{
  // Set up context
  context = Context::create();
}

// I simply used the first draw function to demonstrate it for test (makes no real sense in a real app to do the context init there)
class CClassViewMenuButton : public CMFCToolBarMenuButton
{
	// ... unchanged

	virtual void OnDraw(CDC* pDC, const CRect& rect, CMFCToolBarImages* pImages, BOOL bHorz = TRUE,
		BOOL bCustomizeMode = FALSE, BOOL bHighlight = FALSE, BOOL bDrawBorder = TRUE, BOOL bGrayDisabledButtons = TRUE)
	{

           if (!context) createContext();  // insert to ensure, that transfer area of the DLL has link to optix.6.0.0.dll                                                                    

            // ... unchanged 
	}
};

// ...  all other in file unchanged

The project fully builds and runs (NOTE: copy optix.6.0.0.dll into the same dir where MFCApplication1.exe is)

NOTE:
#include “C:\ProgramData\NVIDIA Corporation\OptiX SDK 6.0.0\SDK\optixTutorial\tutorial.h”
contains device functions for the tutorialXX.cu files not for host code.(so do not include in host code)

if you mix that code, use:

#if defined(__cplusplus)
  // C++ code
#endif

#ifdef __CUDACC__
  __forceinline__ __device__ void myfunction()
  {
    ...
  }
#endif

Thanks m1, I just tried it in your way, and it worked! All compile error gone, now I can continue my work.
THANKS!!!