Problems with afxdb.h

T

Tim

Hi,
I am writing a console app in C++ that is going to be
communicating with an MS SQL Server Database. I was going
to use ODBC to do this and read that I need to include the
afxdb.h header. When I do this, I get the following
linker errors:

nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl
operator new(unsigned int)" (??2@YAPAXI@Z) already defined
in libcpd.lib(newop.obj)
nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl
operator delete(void *)" (??3@YAXPAX@Z) already defined in
LIBCD.lib(dbgdel.obj)
nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl
operator delete[](void *)" (??_V@YAXPAX@Z) already defined
in LIBCD.lib(delete2.obj)
nafxcwd.lib(thrdcore.obj) : error LNK2019: unresolved
external symbol __endthreadex referenced in function "void
__stdcall AfxEndThread(unsigned int,int)" (?
AfxEndThread@@YGXIH@Z)
nafxcwd.lib(thrdcore.obj) : error LNK2019: unresolved
external symbol __beginthreadex referenced in
function "public: int __thiscall CWinThread::CreateThread
(unsigned long,unsigned int,struct _SECURITY_ATTRIBUTES
*)" (?
CreateThread@CWinThread@@QAEHKIPAU_SECURITY_ATTRIBUTES@@@Z)

Currently that is the only library being included, am I
supposed to include anything else? Or is there something
better to use than ODBC?

Thanks,
Tim
 
R

Roy Fine

Tim,

to use the MFC ODBC classes in a console app, you must first initialize
MFC -- that means instantiating a CWinApp object, then calling AfxWinInit...
For that, you will need quite a bit more than just afxdb.h. Here is a code
hack - the stdafx.h and minimal console app with MFC support to instantiate
the CDatabase and CRecordset objects.

/*
** stdafx.h : include file for standard system include files.
*/

#pragma once
#define WINVER 0x0500
#define _WIN32_WINNT 0x0500
#include <iostream>
#include <tchar.h>
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS
#include <afx.h>
#include <afxwin.h>
#include <afxext.h>
#include <afxdb.h>

/*
** console.cpp : Defines the entry point for the console application.
*/

#include "stdafx.h"
#include "console.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace std;
/* The one and only application object */
CWinApp theApp;
CString strDSN("DSN=OraDevl;UID=RLFINE;PWD=EIEIO;");
DWORD dbOpenFlags = CDatabase::blush:penReadOnly|CDatabase::noOdbcDialog;
CDatabase *oraODBC_DB;

/* **************************************************** */
/* **************************************************** */
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){
AfxWinInit:):GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);
oraODBC_DB = new CDatabase;
oraODBC_DB->OpenEx(strDSN,dbOpenFlags);
{
CRecordset rs(oraODBC_DB);
/* .... */
/* .... */
rs.Close();
}
delete oraODBC_DB;
return 0;
}
/* ********* end o-hack ************ */



For the second part of your post, you could use ADO instead of the MFC ODBC
classes -- my personal preference is ADO because I don't like the hassles of
maintaining the ODBC Data Source. Instead of afxdb, you would use the
#import compiler directive to construct a set of helper classes from the ADO
typelib. Here is a link to get you started:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/
pg_ado_programming_8.asp


regards
roy fine


Tim said:
Hi,
I am writing a console app in C++ that is going to be
communicating with an MS SQL Server Database. I was going
to use ODBC to do this and read that I need to include the
afxdb.h header. When I do this, I get the following
linker errors:

nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl
operator new(unsigned int)" (??2@YAPAXI@Z) already defined
in libcpd.lib(newop.obj)
nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl
operator delete(void *)" (??3@YAXPAX@Z) already defined in
LIBCD.lib(dbgdel.obj)
nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl
operator delete[](void *)" (??_V@YAXPAX@Z) already defined
in LIBCD.lib(delete2.obj)
nafxcwd.lib(thrdcore.obj) : error LNK2019: unresolved
external symbol __endthreadex referenced in function "void
__stdcall AfxEndThread(unsigned int,int)" (?
AfxEndThread@@YGXIH@Z)
nafxcwd.lib(thrdcore.obj) : error LNK2019: unresolved
external symbol __beginthreadex referenced in
function "public: int __thiscall CWinThread::CreateThread
(unsigned long,unsigned int,struct _SECURITY_ATTRIBUTES
*)" (?
CreateThread@CWinThread@@QAEHKIPAU_SECURITY_ATTRIBUTES@@@Z)

Currently that is the only library being included, am I
supposed to include anything else? Or is there something
better to use than ODBC?

Thanks,
Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top