Why does including string in my DLL cause so many errors?

Well the title says it, I've got a DLL I'm building but I'm hung up already. Whenever I #include<string> I get over 150 errors. I can #include<string.h> just fine but that's not what I want.
I'll include my code below and a sample of 5 or so errors. If you want more let me know, otherwise there's just too many to include them all.

Header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the TODO: MYTESTDLL_EXPORTS
// DLL software version number

//Software Version Key = MAJOR REVISION . Year / Month / Day / Daily Revision #
#define DLLVersion    1.1107270

// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see 
// MYTESTDLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef AXGEN2LIB_EXPORTS
#define AXGEN2LIB_API /*__declspec(dllexport)*/
#else
#define AXGEN2LIB_API __declspec(dllimport)
#endif

//Exported functions

//Open AXGEN2 DLL Library, and send ?SelectDevice?
//function to open a USB device; return success(1) or fail(0)
int OpenDevice();
//Close previously opened USB device; return success(1) or fail(0)
int CloseDevice();
//Write a feature report to a USB device; return success(1) or fail(0)
int WriteFeature();
//Read a feature report from a USB device; return success(1) or fail(0)
int ReadFeature();


Implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// AXGEN2LIB.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "AXGEN2LIB.h"
#include <new>
#include <cstdlib>
#include <string>

extern "C" {

	// This file is in the Windows DDK available from Microsoft.
	#include "hidsdi.h"

	#include <setupapi.h>
	#include <dbt.h>
}

#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

AXGEN2LIB_API int OpenDevice()
{
 return(FALSE);
}

AXGEN2LIB_API int CloseDevice()
{
return(FALSE);
}

AXGEN2LIB_API int WriteFeature()
{
	return(FALSE);
}

AXGEN2LIB_API int ReadFeature()
{
	return(FALSE);
}


Some Errors:
iosfwd(34) : error C2146: syntax error : missing ';' before identifier 'fpos_t'
iosfwd(219) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
xutility(2971) : error C2143: syntax error : missing ';' before ''template<''
xstring(453) : error C2182: '__CLRCALL_PURE_OR_CDECL' : illegal use of type 'void'
Anybody have any ideas for me? I'm still struggling with this issue.
What's in stdafx.h?
Start the project over as a blank project - stdafx.h is a known evil that causes problems like this. You can copy past the code just leave out stdafx.h!
Well, stdafx.h is used all the time in Windows development, so I don't think it should automatically get *all* the blame!

(Usually stfafx.h contains all external (to the project) includes. All the common Windows SDK files, etc. When pre-compiled first turned up they were a real blessing, and they are still for large projects.)

Googling for a couple of your warnings, I did see people talking about VC++2010 related changes causing problems. Is this the compiler you're using?

When I've hit problems like that, where the errors don't make a lot of sense, I have a look at the preprocessor output to see if I can see what's going on. Have you check it out yet?

If no one here, or elsewhere, can provide you with a solution, it just might be worth a go?
Last edited on
I'm using VC++ 2005 so the 2010 thing shouldn't be it, thanks for looking for me!
I'm not sure I know how to view the preprocessor output, I had read about doing that so I'll look into it again.

Right now, my stdafx.cpp just includes stdafx.h.
The stdafx.h contents are below, but I think I may need some of the declarations for my functions to work? Specifically I think I may need windows.h (I'm using the USB HID library and setupapi.h)

stdafx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER				// Allow use of features specific to Windows XP or later.
#define WINVER 0x0501		// Change this to the appropriate value to target other versions of Windows.
#endif

#ifndef _WIN32_WINNT		// Allow use of features specific to Windows XP or later.                   
#define _WIN32_WINNT 0x0501	// Change this to the appropriate value to target other versions of Windows.
#endif						

#ifndef _WIN32_WINDOWS		// Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif

#ifndef _WIN32_IE			// Allow use of features specific to IE 6.0 or later.
#define _WIN32_IE 0x0600	// Change this to the appropriate value to target other versions of IE.
#endif

#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h> 
I just tried commenting out all of stdafx.h and simply putting #include <string> in and it caused the same errors as before.
The same thing happens with #include <iostream>

Could any of these headers be causing issues?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdlib>
#include <stdlib.h>
#include <wtypes.h>
#include <initguid.h>
#include <cstring>

extern "C" {

	// This file is in the Windows DDK available from Microsoft.
	#include "hidsdi.h"

	#include <setupapi.h>
	#include <dbt.h>
}
I have to idea. Luckily I've never hit this problem!

To generate the preprocessor output, open up the project options and find the C/C++ -> Preprocessor tab.

There is an option "Generate Processed File". Set this to "With line numbers (/P)" and Apply.

Then select one .cpp file (AXGEN2LIB.cpp, in your case) and compile it. It will create a .i file in the same folder as your source (AXGEN2LIB.i). It can take a while if there are loads of headers to process.

And I strongly suggest you reset "Generate Processed File" back to "No" immediately afterwards, so you don't forget about it; the compiler doesn't create .obj file when /P is set. If you forget you'll end up with "unexpected" link problems!


Commenting out stdafx.h include makes VS even angrier at you. Please read my previous post, I stated that you needed to start a blank project and copy the code into it without stdafx.h
^It's actually just a setting in the project options as to whether or not to look for that file (and its name).
Creating a new, blank project did not help unfortunately. I noticed even with stdafx.h still in the project that putting the #include <string> above all other include statements in my header removed the errors, however declaring a string still gave me an error saying that string was an unknown identifier.

I looked at the preprocessor output but couldn't make much of it...there's close to 300k lines of code in it.

I may just have to use c strings :/
When you say you couldn't find "string", you do mean "std::string", yes?

(the code you posted doesn't have a "using namespace std;". Or is it elsewhere?)

Andy

P.S. The old string class (<string.h>, pre standardization) didn't use the std namespace. The new one (<string>) does.
Topic archived. No new replies allowed.