error C2062 when compiling - c++

first I want to say that i'm totally a noob in C or C++. I'm trying to understand how compiling works, how the language works etc. This time I've been looking for a solution for many hours before posting here. I hope you will be able to help me, even if it appears to be a very easy findable solution.
Here it is.
I'm trying to nmake a makefile.Win32 file and I got these errors :
e:\progs\c\vanitygen-master\winglue.h(47) : error C2062: type 'char' unexpected
e:\progs\c\vanitygen-master\winglue.h(47) : error C2143: syntax error : missing ';' before '{'
e:\progs\c\vanitygen-master\winglue.h(47) : error C2447: '{' : missing function header (old-style formal list?)
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\cl.EXE"' : code retour '0x2'
Stop.
Here is the winglue.h file (--> points the line 47)
#include <windows.h>
#include <tchar.h>
#include <time.h>
#define INLINE
#define snprintf _snprintf
struct timezone;
extern int gettimeofday(struct timeval *tv, struct timezone *tz);
extern void timeradd(struct timeval *a, struct timeval *b,
struct timeval *result);
extern void timersub(struct timeval *a, struct timeval *b,
struct timeval *result);
extern TCHAR *optarg;
extern int optind;
extern int getopt(int argc, TCHAR *argv[], TCHAR *optstring);
extern int count_processors(void);
#define PRSIZET "I"
static inline char *
/* --> */ strtok_r(char *strToken, const char *strDelimit, char **context) {
return strtok_s(strToken, strDelimit, context);
}
#endif /* !defined (__VG_WINGLUE_H__) */
I hope you guys will help me !
I'm using Visual Studio C++ 2010 Express on a Win 7 64bits computer.
Edit : If it helps to know it, I'm running a brand new installation of the software.

The simple fix is to make it a define, you are calling a different function with the same parameters in the same order, returning the result.
But as to the cause of your problem, its probalbly a stupid define in <windows.h>, it has alot of defines it shouldnt. Im guessing its inline. try __inline__ instead.

Related

Calling C++ functions from a C src

I have mixed code base of C/C++ in a MS Visual Studio 2010 project and am trying to call a static function defined in C++ file from a C src file. Right now I get it to work by renaming the C src to CPP (a.c -> a.cpp) Just wondering if there's a more graceful way of getting around it (turning some magic compiler flags on etc) without doing any large scale surgery to the code base(like using opaque pointers as suggested in this thread)
Pls note my code base is pretty complex, I have created this small VS snippet to reproduce the error with bare minimum demonstrable code
a.c
#include "b.h"
void test()
{
B::func();
}
b.h
#ifdef __cplusplus
extern "C" {
#endif
class B{
public:
static void func();
};
#ifdef __cplusplus
}
#endif
b.cpp
#include "b.h"
#ifdef __cplusplus
extern "C" {
#endif
void B::func()
{
return;
}
#ifdef __cplusplus
}
#endif
Error:- In MS Visual Studio 2010
1>c:\.....\b.h(5): error C2061: syntax error : identifier 'B'
1>c:\.....\b.h(5): error C2059: syntax error : ';'
1>c:\.....\b.h(5): error C2449: found '{' at file scope (missing function header?)
1>c:\.....\b.h(8): error C2059: syntax error : '}'
First, :: is not valid in C.
Second, including a header is equivalent to copy-pasting a .h file into your C file. Your header must be valid C. Here's some deeper insight:
How to call C++ function from C?
Elegantly call C++ from C
Though, my alternative advice is, compile your C as C++. There's a chance it would take minimal or no work to turn out as valid C++.

missing typedef in header file causes compile error

I have a header file monitor.hpp with:
#ifndef MONITOR_HPP_
#define MONITOR_HPP_
typedef unsigned short abc_status_id_t;
struct monitor_update
{
monitor_update(BYTE* data, size_t size) { /* implementation */ }
BYTE* data;
size_t dataSize;
};
class monitor_consumer
{
public:
virtual ~monitor_consumer() {};
virtual void updated(const monitor_update& update) = 0;
};
#endif // MONITOR_HPP_
Notice above that there is no typedef for the BYTE - long story - but other files using maybe have included Windows.h or something that would have typedef'd BYTE.
But then I have this class where I need to #include that header file:
#ifndef BYTE
typedef unsigned char BYTE;
#endif
#include "monitor.hpp"
class mymonitor : public monitor_consumer
{
public:
void updated(const monitor_update& update) { }
};
int main() {
}
If I comment out the #ifndef BYTE then I get:
monitor.hpp(9): error C2061: syntax error : identifier 'BYTE'
monitor.hpp(10): error C2143: syntax error : missing ';' before '*'
monitor.hpp(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
The define fix works and I don't get any problems compiling and linking. But is it the best approach. What other options do I have?
It seems as if some of the headers you are using do a #define BYTE <something>, which is a fairly unfriendly thing to do.
You could try to find out if it is possible to remove or disable that #define (some Windows headers allow you to selectively turn off parts of them).
Otherwise your solution is a reasonable way of coping with a hostile environment. An alternative would be
#undef BYTE
typedef unsigned char BYTE;

Syntax error C2059 when building building C application using C/C++ DLL header

I'm attempting to translate a C++ DLL header file into a C/C++ compatible header. While I've gotten most of the major constructs in, I'm running into one last compiler issue I can't seem to explain. The following code works fine in C++ but when I attempt to compile a C application which just includes this file I get errors for my function definitions in my header file.
Code.h:
typedef void *PVOID;
typedef PVOID HANDLE;
#define WINAPI __stdcall
#ifdef LIB_EXPORTS
#define LIB_API __declspec(dllexport)
#else
#define LIB_API __declspec(dllimport)
#endif
struct ToolState
{
HANDLE DriverHandle;
HANDLE Mutex;
int LockEnabled;
int Type;
};
#ifdef __cplusplus
extern "C" {
#endif
(LIB_API) int SetRate(ToolState *Driver, int rate);
(LIB_API) void EnableLock(ToolState *Driver) ;
(LIB_API) int SendPacket(ToolState *Driver, unsigned char *OutBuffer, int frameSize);
//These also give me the same error:
//LIB_API WINAPI int SendPacket(ToolState *Driver, unsigned char *OutBuffer, int frameSize);
//__declspec(dllimport) WINAPI int SendPacket(ToolState *Driver, unsigned char *OutBuffer, int frameSize);
//Original C++ call that works fine with C++ but has multiple issues in C
//LIB_API int SetRate(ToolState *Driver, int rate);
#ifdef __cplusplus
}
#endif
Errors:
error C2059: syntax error : 'type'
error C2059: syntax error : 'type'
error C2059: syntax error : 'type'
Google searching hasn't generated any relevant results. The following threads were close but don't exactly answer my question:
C2059 syntax error using declspec macro for one function; compiles fine without it
http://support.microsoft.com/kb/117687/en-us
Why is this syntax error occuring?
In C, structs are not types, so you must use struct Foo and enum Bar where in C++ you are able to use Foo and Bar.
Notes:
In C++, you can still use the old syntax even when the type is a class.
In C, people often use typedef struct Foo Foo which allows the same syntax as in C++ then.

Error C2059: syntax error : 'string'

I have looked at other posts and to be honest I am still not sure what is causing the problem. I am programming in Visual Studio and
I have the following code: (this is a C main)
int main(int arc, char **argv) {
struct map mac_ip;
char line[MAX_LINE_LEN];
char *arp_cache = (char*) calloc(20, sizeof(char)); //yes i know the size is wrong - to be changed
char *mac_address = (char*) calloc(17, sizeof(char));
char *ip_address = (char*) calloc(15, sizeof(char));
arp_cache = exec("arp -a", arp_cache);
It uses the following cpp code:
#include "arp_piping.h"
extern "C" char *exec(char* cmd, char* arp_cache, FILE* pipe) {
pipe = _popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL) {
strcat(arp_cache, buffer);
}
}
_pclose(pipe);
return arp_cache;
}
With the matching header file:
#ifndef ARP_PIPING_H
#define ARP_PIPING_H
#endif
#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif
#include <stdio.h>
#include <string.h>
extern "C" char *exec(char* cmd, char* arp_cache, FILE* pipe);
#undef EXTERNC
But I keep on getting the following errors:
1>d:\arp_proto\arp_proto\arp_piping.h(14): error C2059: syntax error : 'string'
1>main.c(22): warning C4013: 'exec' undefined; assuming extern returning int
1>main.c(22): warning C4047: '=' : 'char *' differs in levels of indirection from 'int'
Please can I get some help, I have looked at other posts regarding the c2059 but am still getting nowhere
Change your exec declaration to use the EXTERNC macro you have taken pains to define.
EXTERNC char *exec(char* cmd, char* arp_cache, FILE* pipe);
I ran into this compilation error when adding an enum to a project. It turned out that one of the values in the enum definition had a name clash with a preprocessor #define.
The enum looked something like the following:
// my_header.h
enum Type
{
kUnknown,
kValue1,
kValue2
};
And then elsewhere there was a #define with the following:
// ancient_header.h
#define kUnknown L"Unknown"
Then, in a .cpp somewhere else in the project, both of these headers were included:
// some_file.cpp
#include "ancient_header.h"
#include "my_header.h"
// other code below...
Since the name kUnknown was already #define'd, when the compiler came to the kUnknown symbol in my enum, it generated an error since the symbol was already used to define a string. This caused the cryptic syntax error: 'string' that I saw.
This was incredibly confusing since everything appears to be correct in the enum definition and compiles just fine on it's own.
It didn't help that this was in a very large C++ project, and that the #define was being transitively included in a completely separate compilation unit and was written by someone 15 years ago.
Obviously, the right thing to do from here is rename that terrible #define to something less common than kUnknown, but until then, just renaming the enum value to something else works as a fix, e.g.:
// my_header.h
enum Type
{
kSomeOtherSymbolThatIsntDefined,
kValue1,
kValue2
};
Anyway, hopefully this answer is helpful for someone else, since the cause of this error stumped me for a good day and a half.
extern "C" is used to tell the compiler to make it as C grammer, but your mean is to declear a extern function called exec. you just make fusion to the differ of this. so rewrite your code like this in arp_piping.h:
/*extern "C"*/ char *exec(char* cmd, char* arp_cache, FILE* pipe);
and then del the preffix of extern "C" in cpp file.
if you want to comiler them with C grammer, just setting in the cpp which call for the function exec, so write like this:
extern "C" {
#include "arp_piping.h"
}

C++ dll in C program

I'd like to create a dll library from C++ code and use it in C program.
I'd like to export only one function:
GLboolean load_obj (const char *filename, GLuint &object_list);
Header file from library:
#ifndef __OBJ__H__
#define __OBJ__H__
#include <windows.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glu.h>
#include <GL/glut.h>
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
extern "C" GLboolean load_obj (const char *filename, GLuint &object_list);
#endif // __3DS__H__
in .cpp (in library project) function is also declared as:
extern "C" GLboolean load_obj (const char *filename, GLuint &object_list)
{
code...
}
File .lib is added in VS project options (Linker/Input/Additional dependencies). .dll is in folder where .exe is.
When I compile C project - error:
Error 1 error C2059: syntax error : 'string'
It is about part "extern "C" " in header file.
I've tried to change header file to:
extern GLboolean load_obj (const char *filename, GLuint &object_list);
then
Error 1 error C2143: syntax error : missing ')' before '&'
Error 2 error C2143: syntax error : missing '{' before '&'
Error 3 error C2059: syntax error : '&'
Error 4 error C2059: syntax error : ')'
and even when I changed & to * appeared:
Error 6 error LNK2019: unresolved external symbol _load_obj referenced in function _main main.obj
I've no idea why it is wrong. .lib .h and .dll are properly added.
The parameter "GLuint &object_list" means "pass a reference to an GLuint here". C doesn't have references. Use a pointer instead.
// declaration
extern "C" GLboolean load_obj (const char *filename, GLuint *object_list);
// definition
GLboolean load_obj (const char *filename, GLuint *object_list)
{
code...
}
C has no references, as David pointed out.
In addition, take out extern "C". C does not have a use for nor know about it.
If you need to share the header, do something like:
#ifdef __cplusplus
extern "C" {
#endif
/* extern "C" stuff */
#ifdef __cplusplus
}
#endif
In C, __cplusplus won't be defined.