C++ dll in C program - c++

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.

Related

LNK2019 unresolved external appears when function is used inside static method

I have a C++ Visual Studio 2013 console application which is supposed to make use of a DLL MyDLLlib.dll which I have written. MyDLLlib is written in C. One of the functions is called Get_Version. The prototype is
const char *Get_Version();
I put this at the top of the source files to make use of the prototype:
extern "C"{
#include "MyDLLlib.h"
}
If in the function is called in the main as this
printf("version %s\n",Get_Version());
then it works.
However if I add a class with some static methods and a static method makes a call to Get_Version()
const char * ret = Get_Version();
then I get a link error:
Error 1 error LNK2019: unresolved external symbol
"__declspec(dllimport) char * __cdecl Get_Version(void)" (__imp_?Get_Version##YAPADXZ)
referenced in function "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl ServiceDispatch::decoder_Get_Version(class StringBuffer &)"
(?decoder_Get_Version#ServiceDispatch##CA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##AAVStringBuffer###Z)
D:\devt\CplusPlus\VSTrials\Link_to_MyDLLlib\Link_to_MyDllLib\ServiceDispatch.obj Link_to_MyDLLlib``
I am using the same include.
Any clue as to what I might be doing wrong?
If you have CLASS_DECLSPEC defined always as __declspec(dllimport), this will not work for sure. Look at this sample:
DLL_header.h
#if defined( _BUILD_DLL )
# define DLLAPI __declspec(dllexport) //Export when building DLL
#else
# define DLLAPI __declspec(dllimport) //Import when using in other project
#endif
DLLAPI const char *Get_Version();
DLL_source.cpp
#include "Header.h"
const char *Get_Version()
{
return "1.1.0.4";
}
Build DLL with _BUILD_DLL defined.
Main.cpp
#include "DLL_header.h"
int main()
{
printf("%s\n", Get_Version());
return 0;
}
Build this, with _BUILD_DLL not defined.
In your case, it could be problem with extern "C" - you include header inside extern "C", which declares Get_Version() as having __cdecl linkage. But linker is searching for
__imp_?Get_Version##YAPADXZ
Which is a mangled (C++) name. Is your DLL a C or C++ project? If your DLL is build as C project (not C++), put extern "C" on Get_Version()'s declaration with this #ifdef:
#ifdef __cplusplus
extern "C" {
#endif
DLLAPI const char *Get_Version();
#ifdef __cplusplus
}
#endif
Either way, remove extern "C" from around the #include. Also, check if .lib file for this DLL is attached to project as dependency.

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 C2062 when compiling

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.

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"
}

Use bison and flex with vc6

When i use bison & flex with vc6, i got got below errors
lex.yy.c(395) : error C2146: syntax error : missing ';' before identifier 'YY_PROTO'
lex.yy.c(395) : fatal error C1004: unexpected end of file found
what would be the cause for this??
please help.
Copied from Comment:
#ifndef YY_SKIP_YYWRAP
#ifdef __cplusplus
extern "C" int yywrap YY_PROTO(( void ));
#else
extern int yywrap YY_PROTO(( void ));
#endif
#endif
The YY_PROTO macro is only to support old pre-standard C without support for prototypes. You will have hard to find a compiler that does not support that today. That means that as a first debugging step you could try to remove it completely since you want to use prototypes, i.e. modify lex.yy.c to the following:
#ifndef YY_SKIP_YYWRAP
#ifdef __cplusplus
extern "C" int yywrap ( void );
#else
extern int yywrap ( void );
#endif
#endif
I know that lex.yy.c is a generated file, so that will not be a permanent fix, but it should at least confirm that the problem is related to the definition of YY_PROTO.
YY_PROTO is a macro that is defined earlier in the same file, so something odd is going on near the macro definition. Search earlier in the file to see how YY_PROTO is defined -- if its not getting defined, your compiler is doing something very weird.