When I compile my project I get this error.
C:\src\libs\nvrtpaudio\FileRtpSource.
cpp(61) : error C3861: 'timeBeginPeriod': identifier not found
C:\src\libs\nvrtpaudio\FileRtpSource.
cpp(71) : error C3861: 'timeEndPeriod': identifier not found
gmake[5]: *** [_out/win7_x86_debug/FileRtpSource.obj] Error 2
I included windows.h but this error still persists. Anyone know how to resolve this?
MSDN says:
Header: Mmsystem.h (include Windows.h)
So you are expected to include "windows.h" and be fine, but what MSDN does not say is that this assumes you don't have WIN32_LEAN_AND_MEAN defined, which - when defined, and this also can be the case with project created from template - exlcudes "mmsystem.h" you need.
So you have to either make sure you don't have WIN32_LEAN_AND_MEAN in your project, or otherwise include directly:
#include "stdafx.h"
#include <mmsystem.h> // <<--- Here we go
#pragma comment(lib, "winmm.lib")
int _tmain(int argc, _TCHAR* argv[])
{
timeBeginPeriod(0);
return 0;
}
Related
I am using Visual Studio 2005. I created an MFC based console application named "StdAfx dependancy". The IDE created the following files for me.
Resource.h
StdAfx Dependancy.h
stdafx.h
StdAfx Dependancy.cpp
stdafx.cpp
I added another class CHelper with Helper.h and Helper.cpp as below.
Helper.h:
#pragma once
class CHelper
{
public:
CHelper(void);
~CHelper(void);
};
Helper.cpp
#include "StdAfx.h"
#include "Helper.h"
CHelper::CHelper(void)
{
}
CHelper::~CHelper(void)
{
}
I created an object for CHelper in the main function; to achieve this I added Header.h file in the first line of StdAfx Dependancy.cpp as below; and I got the following errors.
d:\codes\stdafx dependancy\stdafx
dependancy\stdafx dependancy.cpp(33) :
error C2065: 'CHelper' : undeclared
identifier
d:\codes\stdafx
dependancy\stdafx dependancy\stdafx
dependancy.cpp(33) : error C2146:
syntax error : missing ';' before
identifier 'myHelper'
d:\codes\stdafx
dependancy\stdafx dependancy\stdafx
dependancy.cpp(33) : error C2065:
'myHelper' : undeclared identifier
But when I include it after stdafx.h, the error vanishes. Why?
// Stdafx dependancy.cpp : Defines the entry point for the console application.
//
#include "Helper.h"
#include "stdafx.h"
#include "Stdafx dependancy.h"
// #include "Helper.h" --> If I include it here, there is no compilation error
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
CHelper myHelper;
}
return nRetCode;
}
This link must give you some clue.
Purpose of stdafx.h
The lines defined before the
#include "stdafx.h" are ignored by the compiler. So if you want to actually include those files then you need to include them after the #include "stdafx.h".
Hope it is clear.
In addition to ckv's answer, it makes little sense to include header files before "stdafx.h" as any non-trivial header file will contain framework types that are included there (e.g. any MFC types).
I am trying to run a project using Visual studio 2003. But I am getting lot of compilation errors similar to the following.
The errors are pointing to WinSock2.h file. I am copying couple of code snippets from WinSock2.h file and the corresponding errors
typedef struct fd_set {
u_int fd_count; /* how many are SET? */
SOCKET fd_array[FD_SETSIZE]; /* an array of SOCKETs */
} fd_set;
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\PlatformSDK\Include\WinSock2.h(114): error C2065: 'fd_set' :
undeclared identifier
struct sockaddr {
u_short sa_family; /* address family */
char sa_data[14]; /* up to 14 bytes of direct address */
};
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\PlatformSDK\Include\WinSock2.h(109): error C2143: syntax
error : missing ';' before '{'
The ws2_32.lib file is added to "Configuration properties - Linker - Input - Additional Dependencies". The build configuration platform is win32.
Thanks in advance for your help.
a typical basic Winsock Application with the good order of header files can be found here:
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
int main() {
return 0;
}
The order of including header files is important
My code is as follows
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include <windows.h>
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
BYTE* pAlloc1 = NULL;
return 0;
}
creating following errors.
error C2065: 'BYTE' : undeclared identifier
What am I doing wrong here?
You have #include "stdafx.h", which usually means that you're using a precompiled header. If you use a precompiled header, anything preceding the precompiled header will be discarded.
Try reordering your #include lines so that "stdafx.h" is first. (Or change stdafx.h to #include <windows.h>, which is generally where you want to put commonly-used system headers.)
I'm getting this error from compilator:
1>Linking...
1>main.obj : error LNK2005: "int g_win_flags" (?g_win_flags##3HA) already defined in init.obj
1>main.obj : error LNK2005: "struct SDL_Surface * g_screen" (?g_screen##3PAUSDL_Surface##A) already defined in init.obj
1>MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
1>.\Debug\Heroes are back!.exe : fatal error LNK1169: one or more multiply defined symbols found
It looks like that g_win_flags and g_screen are twice included, but I don't understand why.
Here is the source:
main.cpp
#include <iostream>
#include "dec.h"
#include "init.h"
int main(int argc, char *argv[]){
init();
return 0;
}
dec.h
#ifndef DEC_H
#define DEC_H
#include <SDL.h>
#include <iostream>
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
using namespace std;
int g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
SDL_Surface *g_screen = NULL;
#endif
init.h
#ifndef INIT_H
#define INIT_H
bool init();
#endif
init.cpp
#include "dec.h"
bool init(){
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) == -1){
cerr << "Unable to initialize SDL" << endl;
return false;
}
g_screen = SDL_SetVideoMode(640, 480, 0, g_win_flags);
return true;
}
Can someone help? Thanks in advance and have a nice day :)
You define and initialize the variables in the header.
You should just declare them in the header (dec.h) without any initializers:
extern int g_win_flags;
extern SDL_Surface *g_screen;
Then define them once in one file - presumably dec.cpp - with the initializations.
As it was, you were defining them in every source file that included 'dec.h' and then running foul of the ODR - One Definition Rule.
in dec.h you want
extern int g_win_flags;
extern SDL_Surface *g_screen;
and then to define and initalise them in just dec.cpp
Update:
#include "dec.h"
int g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
SDL_Surface *g_screen = NULL;
The general rule of thumb is "nothing in a header file should take up any space in the output of the compiler". (There are exceptions to this obviously)
In practice this means extern variable declarations are fine, as are function declarations, but not definitions.
You have included files into two different source files (init.cpp and main.cpp) that define instantiated variables.
You need a way to be sure they are 'externed' in all but one source file.
Well, I tried to do what you've told me guys, but compilator is complaining:
1>.\heroes are back!\dec.cpp(2) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\heroes are back!\dec.cpp(4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\heroes are back!\dec.cpp(4) : error C2040: 'g_screen' : 'int' differs in levels of indirection from 'SDL_Surface *'
Here is the dec.h
#ifndef DEC_H
#define DEC_H
#include <SDL.h>
#include <iostream>
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
using namespace std;
extern int g_win_flags;
extern SDL_Surface *g_screen;
#endif
dec.cpp
#include "dec.h"
g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
g_screen = NULL;
What is wrong in this code? Sorry for asking dumb questions, but I'm only learning C++ :)
I got a strange compilation error when I followed the MSDN document to use CA2W to convert big5 strings to unicode strings in Visual Studio 2005.
This is the code I wrote:
#include <string>
#include <atldef.h>
#include <atlconv.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string chineseInBig5 = "\xA4\xA4\xA4\xE5";
ATL::CA2W(chineseInBig5.c_str());
return 0;
}
The compilation error: error C3861: 'AtlThrowLastWin32': identifier not found
I don't know how this could happen. The document of AtlThrowLastWin32 shows that atldef.h is required, but I couldn't find the declaration of AtlThrowLastWin32 in atldef.h.
I finally solved this problem by adding 2 include headers:
#include <atlbase.h>
#include <atlstr.h>
I don't know why the MSDN document doesn't mention that.