Error compiling metismex in matlab r2013 - c

I have downloaded and succesfully compiled metis 5.0.2 in a win7 x64 pc
and trying to compile metismex.
I compiled metis with Visual Studio 11 (2012) and using the same compiler from within matlab.
After a lot of experimentation with a ton of errors (mainly owed to paths issues from within the libraries) I have reached a point where I dont know how to proceed, since my knowledge on c and c++ is pretty basic.
So, here's the error :
../GKlib/mat_libs/.\stddef.h(16) : error C2054: expected '(' to follow '_SIZE_TYPE__'
../GKlib/mat_libs/.\stddef.h(19) : error C2085: '_WCHAR_TYPE__' : not in formal parameter list
I found out about the inline functions etc, but since the error is within a library and dont exactly know what I should be doing, here I am. So, the error code is produced here :
typedef __SIZE_TYPE__ size_t;
#ifndef __cplusplus
typedef __WCHAR_TYPE__ wchar_t;
#endif
any suggestions on what I should do without messing it up?
(in case I comment out wchar type, I also have the same error on prtdiff_type)
Thanks in advance
P.S: In case it is needed, here's the whole sttdef.h
/* Copyright 2012 The MathWorks, Inc. */
#ifndef _STDDEF_H
#define _STDDEF_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
# define NULL (0)
#else
# define NULL ((void *)0)
#endif
typedef __SIZE_TYPE__ size_t;
#ifndef __cplusplus
typedef __WCHAR_TYPE__ wchar_t;
#endif
typedef __PTRDIFF_TYPE__ ptrdiff_t;
#if (! defined(__cplusplus)) || (! defined(PST_GNU))
# define offsetof(type, field) ((size_t) &((type *)0)->field)
#else
# define offsetof(type, field) \
(__offsetof__(reinterpret_cast<size_t> \
(&reinterpret_cast<const volatile char &> \
(static_cast<type *>(0)->field))))
#endif
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* _STDDEF_H *
/

Here is a walk-through of what I did. I'm running R2014a with VS2013 on Win8.1 x64.
1) First we compile metis:
Download and extract metis-5.1.0 to some location, say C:\metis-5.1.0
Edit C:\metis-5.1.0\include\metis.h and set #define IDXTYPEWIDTH 64 (for x64 architecture)
Next we generate Visual Studio projects using CMake:
> cd C:\metis-5.1.0
> vsgen -G "Visual Studio 12 2013 Win64"
Before we build the solution, we need to fix a few things first. Some header files are unnecessarily redefining rint function for MSVC (metisbin.h, metislib.h, and gk_arch.h). Remove such lines:
#ifdef __MSC__ /* MSC does not have rint() function */
#define rint(x) ((int)((x)+0.5))
/* MSC does not have INFINITY defined */
#ifndef INFINITY
#define INFINITY FLT_MAX
#endif
#endif
Also in GKlib\gk_externs.h replace all occurrences of __thread with __declspec(thread)
Next open the solution file C:\metis-5.1.0\build\windows\METIS.sln in Visual Stduio, and build ALL_BUILD target (make sure "x64" in "Release" mode is selected).
We are mainly interested in the metis project. Its result should be stored in C:\metis-5.1.0\build\windows\libmetis\Release\metis.lib (a static library).
2) Next we build the MEX-function:
Download metismex and extract it to a folder inside the previous location (C:\metis-5.1.0\metismex-master)
Again we need to fix a few things: first rename metismex.c to metismex.cpp (the C++ compiler is much better than the C compiler in Visual Studio!). Next edit this file and replace: #include <strings.h> with #include <string.h>, and add the following code immediately after it:
#if defined(_WIN32) || defined(_WIN64)
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif
Finally start MATLAB, and run the following command to compile the MEX-file:
>> cd('C:\metis-5.1.0\metismex-master')
>> mex -O -largeArrayDims -DWIN32 -DMSC -DUSE_GKREGEX -I../GKlib -I../include -I../libmetis metismex.cpp ../build/windows/libmetis/Release/metis.lib
You should now have the final metismex.mexw64
I should say that I know nothing about the library, so I cannot guarantee it gives correct results after all the above modifications. I'm just showing how to get it to compile. The code was written with Linux/OSX in mind, and relies on many POSIX features not intended for Windows. Also the whole 32 vs. 64 bit is a bit messy...

Related

How do I concatenate two string macros in C?

I am trying to implement VERSION macro for my program, that is to be changed under certain circumstances.
macro VERSION is defined via Makefile (git info is put there) and is a string.
Now I have a set of #define'd switches and I want VERSION to reflect which of them are on. This looks now like the follows (main.h):
#define COMPLEX_DEPOSITION // This is switch. later in code it is used in #ifdef...#endif construction.
#ifdef COMPLEX_DEPOSITION
#define CD "_COMP_DEP" // this is the string I want to put in the end of VERSION
#define VERSION_ VERSION CD
#undef VERSION // this is to suppress 'macro redefinition' warning
#define VERSION VERSION_
#undef VERSION_
#endif
Well, I get a lot of errors, most of which make me think that C preprocessor works with lines in file in random order:(
Later I have an even more complex thing that is intended to make VERSION -> VERSION_WLT_GAP_2
#define WIRESLIFETIMES
#ifdef WIRESLIFETIMES
#define GAP 2
#define VERSION_ (VERSION ## "_WLT_GAP_" ## #GAP)
#define VERSION VERSION_
#undef VERSION_
#endif
and I got no idea what to do and if this is even possible
String literals concatenate naturally when placed next to each other
"foo" "bar" is the same as "foobar".
As for the second example, you probably want:
#define CAT_(A,B) A##B
#define CAT(A,B) CAT_(A,B)
#define GAP 2
#define VERSION CAT(VERSION_WLT_GAP_ , GAP)
VERSION //expands to VERSION_WLT_GAP_2
I recommend playing with gcc -E/clang -E a bit, to learn how macros work,
before trying to compose anything complex with them.
Well, the answer seems to be the following:
// https://stackoverflow.com/questions/5256313/c-c-macro-string-concatenation
// Concatenate preprocessor tokens A and B without expanding macro definitions (however, if invoked from a macro, macro arguments are expanded).
#define PPCAT_NX(A, B) A ## B
// Concatenate preprocessor tokens A and B after macro-expanding them.
#define PPCAT(A, B) PPCAT_NX(A, B)
// Turn A into a string literal without expanding macro definitions (however, if invoked from a macro, macro arguments are expanded).
#define STRINGIZE_NX(A) #A
// Turn A into a string literal after macro-expanding it.
#define STR(A) STRINGIZE_NX(A)
#define COMPLEX_DEPOSITION
#ifdef COMPLEX_DEPOSITION
#define CD "_COMPDEP"
#else
#define CD ""
#endif
#define WIRESLIFETIMES
#ifdef WIRESLIFETIMES
#define GAP 2
#define WLT STR(PPCAT(_WLT:G, GAP))
#define DISABLE_METROPOLIS
#else
#define WLT ""
#endif
#define VERSION VERSIONX CD WLT
which produces V008.1-11-g68a9c89cb4-dirty_COMPDEP_WLT:G2 and I am happy with it.
Must be noted that I changed -DVERSION=... to -DVERSIONX=... inside Makefile

Is there a constant that clang recognizes on mac os x? [duplicate]

I am fairly new to C++. I am currently working on a group project and we want to make our classes compatible with both the lab computers (Windows) and my computer (Mac OS X).
Here is what we have been putting at the top of our files:
#ifdef TARGET_OS_X
# include <GLUT/glut.h>
# include <OpenGL/OpenGL.h>
#elif defined _WIN32 || defined _WIN64
# include <GL\glut.h>
#endif
I realize this question has been asked before but my searches have been giving me conflicting answers such as "_MAC", "TARGET_MAC_OS", "MACINTOSH", etc. What is the current and correct declaration to put in the #ifdef statement to make this compatible with Mac? Right now it is not working.
Thank you!
According to this answer:
#ifdef __APPLE__
#include "TargetConditionals.h"
#ifdef TARGET_OS_IPHONE
// iOS
#elif TARGET_IPHONE_SIMULATOR
// iOS Simulator
#elif TARGET_OS_MAC
// Other kinds of Mac OS
#else
// Unsupported platform
#endif
#endif
So in short:
#ifdef __APPLE__
#include "TargetConditionals.h"
#ifdef TARGET_OS_MAC
#include <GLUT/glut.h>
#include <OpenGL/OpenGL.h>
#endif
#elif defined _WIN32 || defined _WIN64
#include <GL\glut.h>
#endif
It depends on the compiler. #ifdef __APPLE__ works for gcc.
According to Microsoft, _WIN32 will cover both the 32-bit and 64-bit versions of Windows. And __APPLE__ works for Clang (at least in Mavericks). So one correct way to write the ifdefs above is:
#ifdef __APPLE__
DoSomething();
#elif _WIN32
DoSomethingElse();
#else
GenerateErrorOrIgnore
Small correction: #ifdef TARGET_OS_MAC will get you always true on both OS X and iOS, as it is defines either 0 or 1 depending on platform, but when APPLE is defined, TARGET_OS_MAC is defined as well, so checking it inside the #ifdef APPLE is worthless.
You might want to use #if TARGET_OS_MAC instead. Same for all TARGET_* macros.

MAC 10.7 : Where is the gl.h file? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
FreeGLUT on Mac OS X Lion
First quizz : could anyone just please explain me why am I not able to find the gl.h file anywhere on my mac ?! If this question can simply be answered maybe you do not even have to read the rest, but I couldn't find any answer on the web to that one (after hours of research), except the famous "it's integrated into the os" or something like that.
My problem In detail:
I am having an issue with the compilation of a .c file using OpenGL. Ihave browsed the web for hours without finding any solution. Here is the problem :
For those who know this software, I am using Webots (a robot simulation software) and I would like to implement a physic plugin. I copied the code from an already existing (and working) plugin, just to try it out. I am compiling with webots, which uses a gcc (I had to install a gcc compiler since it doesn't come with Xcode anymore).
Now, the problem is that the .c file includes a "physics.h" header which includes the OpenGL/gl.h file and I then get the error : "OpenGL/gl.h . no such file or directoy".
Now I've tried to many things :
making a search for gl.h : it seems it doean't exist (I can't find it, either manually or with the finder search function)
replacing the include OpenGL/gl.h by GLUT/glut.h : I get the error "gl.h and glu.h no such fil or directory" since their included in the glut.h
linking the gl libraries in the makefile and the frameworks as well
Probably all the combination of these, using absolute or relative path !
Since OpenGL and GLUT are supposed to be natively installed with OS (I am on using Lion 10.7.5) I guess every needed file should be there. And for this reaon I cannot find a way to download this supposedely missing file...
Moreover a coworker of mine tried and successfully compiled it on linux ! Linux is a great platform but I still love my mac, so please let me hope there is another solution than to change for linux !!!
I think my problem lies either in the Makefile or in the physic.h file, so here they are :
Physic.h:
#ifndef PHYSICS_H
#define PHYSICS_H
#include <ode/ode.h>
#ifdef WIN32
#include <windows.h>
#endif
#ifndef MACOS
#include <GL/gl.h>
#else
#include <OpenGL/gl.h>
#endif
/* callback functions to be implemented in your physics plugin */
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _MSC_VER
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT
#endif
DLLEXPORT void webots_physics_init(dWorldID,dSpaceID,dJointGroupID);
DLLEXPORT int webots_physics_collide(dGeomID,dGeomID);
DLLEXPORT void webots_physics_step();
DLLEXPORT void webots_physics_step_end();
DLLEXPORT void webots_physics_cleanup();
DLLEXPORT void webots_physics_draw();
DLLEXPORT void webots_physics_predraw();
/* utility functions to be used in your callback functions */
#ifndef LINUX
extern dGeomID (*dWebotsGetGeomFromDEFProc)(const char *);
extern dBodyID (*dWebotsGetBodyFromDEFProc)(const char *);
extern void (*dWebotsSendProc)(int,const void *,int);
extern void* (*dWebotsReceiveProc)(int *);
extern void (*dWebotsConsolePrintfProc)(const char *, ...);
extern double (*dWebotsGetTimeProc)();
#define dWebotsGetGeomFromDEF(DEFName) (*dWebotsGetGeomFromDEFProc)(DEFName)
#define dWebotsGetBodyFromDEF(DEFName) (*dWebotsGetBodyFromDEFProc)(DEFName)
#define dWebotsSend(channel,buff,size) (*dWebotsSendProc)(channel,buff,size)
#define dWebotsReceive(size_ptr) (*dWebotsReceiveProc)(size_ptr)
#if defined(__VISUALC__) || defined (_MSC_VER) || defined(__BORLANDC__)
#define dWebotsConsolePrintf(format, ...) (*dWebotsConsolePrintfProc)(format, __VA_ARGS__)
#else
#define dWebotsConsolePrintf(format, ...) (*dWebotsConsolePrintfProc)(format, ## __VA_ARGS__)
#endif
#define dWebotsGetTime() (*dWebotsGetTimeProc)()
#else
dGeomID dWebotsGetGeomFromDEF(const char *DEFName);
dBodyID dWebotsGetBodyFromDEF(const char *DEFName);
void dWebotsSend(int channel,const void *buffer,int size);
void *dWebotsReceive(int *size);
void dWebotsConsolePrintf(const char *format, ...);
double dWebotsGetTime();
#endif
#ifdef __cplusplus
}
#endif
#endif /* PHYSICS_H */
Makefile:
###
### Standard Makefile for Webots physics plugins
###
### Supported platforms: Windows, Mac OS X, Linux
### Supported languages: C, C++
###
### Authors: Olivier Michel - www.cyberbotics.com
### Revised: Yvan Bourquin - September 30th, 2009.
###
### Uncomment the variables to customize the Makefile
### -----C Sources-----
###
### if your plugin uses several C source files:
C_SOURCES = my_contact_p_physics.c
### -----C++ Sources-----
###
### if your plugin uses several C++ source files:
# CPP_SOURCES = my_plugin.cpp my_clever_algo.cpp my_graphics.cpp
### or
# CC_SOURCES = my_plugin.cc my_clever_algo.cc my_graphics.cc
FRAMEWORK = -framework OpenGL -framework GLUT
### -----Options-----
###
### if special CFLAGS are necessary, for example to set optimization level or
### to find include files:
#CFLAGS=-O3 -I/System/Library/Frameworks/OpenGL.framework
###
### if your plugin needs additional libraries:
LIBRARIES=-L/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries -llibGL -llibGLU
### Do not modify: this includes Webots global Makefile.include
space :=
space +=
WEBOTS_HOME_PATH=$(subst $(space),\ ,$(strip $(subst \,/,$(WEBOTS_HOME))))
include $(WEBOTS_HOME_PATH)/resources/projects/default/plugins/physics/Makefile.include
P.S: In the "my_contact_p_physics.c" (the .c file I am trying so hard to compile) I simply include a ODE/ode.h file and the physics.h file with absolute path. I cannot compile directy in the terminal because of the ODE which "integrated" into webots, reason why I compile in webots directly.
The gl.h is there, alright - Just in /System/Library/Frameworks/OpenGL.framework/Headers. Either use -framework OpenGL, or add -I /System/Library/Frameworks/OpenGL.framework/Headers to the compiler flags. Assuming your file is there (which it is, by default), you should find it, and all should work!

Error C2491 on C source with Visual studio 8

i'm really noob in C. I just need to compile a ANSI C source to get a dll.
During compilation i get this error:
C2491: 'SelectML': definition of dllimport function not allowed
Where SelectML is a public function with this definition:
int CALLINGCONV SelectML(WORD fid, int nSlot)
{
WORD SW;
int x;
BYTE pSend[2];
pSend[0]=(BYTE)((fid&0xff00)>>8);
pSend[1]=(BYTE)(fid&0x00ff);
x=SendAPDUML(hCards[nSlot],APDU_SELECT,2,0,pSend,0,&SW);
if (x!=C_OK) return x;
if (SW!=0x9000) return SW;
return C_OK;
}
I'm sure the C source is good, maybe it is just a Visual Studio configuration...
This is another linked header:
#ifndef LIBSIAECARDT_H
#define LIBSIAECARDT_H
#ifdef __cplusplus
extern "C" {
#endif
#if !defined(USE_STDCALL)
#define USE_STDCALL 1
#endif
#ifdef _WIN32
# if USE_STDCALL == 1
# define CALLINGCONV_1 _stdcall
# else
# define CALLINGCONV_1
# endif
# if defined(LIBSIAE_EXPORTS)
# define LIBSIAEAPI __declspec(dllexport)
# else
# define LIBSIAEAPI __declspec(dllimport)
# endif
# define CALLINGCONV LIBSIAEAPI CALLINGCONV_1
#else // ! _WIN32
# define CALLINGCONV
# define LIBSIAEAPI
# define CALLINGCONV_1
typedef unsigned int UINT;
#endif // _WIN32
It's common to have a macro like CALLINGCONV conditionally defined as __declspec(dllimport) or __declspec(dllexport) so that the same header file can be used in the library source and in the code using the library. Your build should probably define something that makes it use dllexport. Check how CALLINGCONV is defined or (preferably) consult any build documentation that came with the code.
Quoted in MSDN says it all. Do not define the function. Declaration is good. What you are doing here is defining SelectML which for sure is generating your C2491 Error.
Here's an alternative. Stop using MSVC. They explicitly dropped support for anything after C90. Use an actual C compiler for C code.

#define DEBUG 1

I'm trying to have a debugging mode on so if
#define DEBUG 1
I want to printf some variable values and if
#define DEBUG 0
I want them off.
The problem is I have many implementation files and I want this DEBUG variable to be available for the whole project. Right now I need to edit the DEBUG variable in foo1.c, foo2.c, foo3.c which seems tedious and error-prone and there must be a better way. Any suggestions?
When compiling, you should be able to specify an option to your compiler. For example, you can call GCC with the -DDEBUG option.
In this case, you would be better using:
#ifdef DEBUG
#endif
or:
#if defined(DEBUG)
#endif
if this is not the way you're doing it now. I'm surprised that you don't have a global header file for your project. Something along the lines of:
#undef DEBUG
#define DEBUG 1
in a file called "debug.h". In your C programs, you can include this by using #include "debug.h"
Try something like Steve McConnel suggests in section 6 of "Chapter 8: Defensive Programming" from Code Complete 2... Add this to your code:
#ifdef DEBUG
#if (DEBUG > 0) && (DEBUG < 2)
printf("Debugging level 1");
#endif
#if (DEBUG > 1) && (DEBUG < 3)
printf("Debugging level 2");
#endif
#if (DEBUG > n-1) && (DEBUG < n)
printf("Debugging level n");
#endif
#endif
Then when you compile, add this flag (warning: This might be compiler-dependent):
-DDEBUG=m
Or, have a global header that defines these sorts of things, as others have suggested.
As a response to your problem you can also simply invoke the compiler like:
cc -c -DDEBUG=1
or
cc -c -DDEBUG=0
You must delete the "define DEBUG 1/0" in your files - or replace it with:
#ifndef DEBUG
#define DEBUG 0
#endif
Here is what I am using (GCC syntax):
create a file debug.h with the following content and include it in each c file:
#ifdef DEBUG
extern FILE *dbgf;
#define D_MIN 0x00010000 // Minimum level
#define D_MED 0x00020000 // Medium level
#define D_MAX 0x00040000 // Maximum level
#define D_FLUSH 0x00080000 // Usefull by a program crash
#define D_TRACE 0x00100000
#define D_1 0x00000001
...
#define D(msk, fmt, args...) if(msk & dbgmsk) { fprintf(dbgf, "%s:",__FUNCTION__); fprintf(dbgf, fmt, ## args ); if(msk & D_FLUSH) fflush(dbgf); }
#define P(msk, fmt, args...) if(msk & dbgmsk) { fprintf(dbgf, fmt, ## args ); if(msk & D_FLUSH) fflush(dbgf); }
#else
#define D(msk, fmt, args...)
#define P(msk, fmt, args...)
#endif
dbgmsk is variable, which can be global (whole program) or local/static and must be initialized a start. You can define several options for the whole program or for each module. This is better and more flexible than the version with the level variable.
Ex.
module1.c:
#include "debug.h"
static int dbgmsk; // using local dbgmsk
module1_setdbg(int msk) { dbgmsk = msk; D(D_TRACE,"dbgmsk1=%x\n", dbgmsk); }
foo1() { P(D_1, "foo1 function\n" );
....
}
foo2() {}
...
foo3.c
#include "debug.h"
extern int dbgmsk; // using global dbgmsk
Ex. main:
#include "debug.h"
FILE *dbgf;
int dbgmsk = 0; // this is the global dbgmsk
int main() {
dbgf = stderr; // or your logfile
dbgmsk = D_MIN;
module1_setdbg(D_MIN|D_MED|D_TRACE|D_1);
....
}
I'm also storing all dbgmsk variables in a config text file that is read at the program start.
As #person-b says, specify this define as a compiler option, e.g. -D DEBUG
Note though that to simplify this you should change the test in your code from:
#if DEBUG
to:
#ifdef DEBUG
This way you don't have to worry about specifying a 0 or 1 value but can instead rely on it being defined or not.
Put the "#define DEBUG" in "debug.h" and #include that header file in each *.c file.
The suggestion from samoz and Stephen Doyle to check for the existence of a definition for DEBUG rather than its value is a good one. However, if you really want to use DEBUG=0, this is how you can do it: Each time you define the DEBUG flag (i.e., in each file), check for an existing definition:
#ifndef DEBUG
#define DEBUG 1
#endif
Then, when you use the option -DDEBUG=0 with your compiler, the #define line will never be executed.
Try this instead.
In the first file you have that will be included:
#define DEBUG
Then whenever you want to have debug code, do this:
#ifdef DEBUG
do some stuff
#endif
This will also prevent your debugging code from making it into release code.
I personally like
#ifdef DEBUG
#define IFDEBUG if(0)else
#else
#define IFDEBUG if(1)else
#endif

Resources