|
From: LDT <in...@ld...> - 2006-10-02 13:22:55
|
-----Original Message-----
From: min...@li...
[mailto:min...@li...] On Behalf Of JC
Denton
Sent: Monday, October 02, 2006 12:03 PM
To: min...@li...
Subject: [Mingw-users] (no subject)
Hi. I'm using Dev-C++ 4.9.9.2 with MinGW (gcc version 3.4.2).
I'm writing an application for accessing Oracle database through ODBC.
For the present I'm just learning methods to access database through
ODBC API, but it's no important. Here is source code of sample
application which uses ODBC (It's copied from Microsoft help):
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <stdlib.h>
#include <mbstring.h>
#define MAX_DATA 100
#define MYSQLSUCCESS(rc)
((rc==SQL_SUCCESS)||(rc==SQL_SUCCESS_WITH_INFO))
class direxec
{
RETCODE rc; // ODBC return code
HENV henv; // Environment
HDBC hdbc; // Connection handle
HSTMT hstmt; & nbsp; // Statement handle
unsigned char szData[MAX_DATA]; // Returned data storage
SDWORD cbData; // Output length of data
unsigned char chr_ds_name[SQL_MAX_DSN_LENGTH]; // Data source name
public:
direxec(); // Constructor
void sqlconn(); // Allocate env, stat, a nd conn
void sqlexec(unsigned char *); // Execute SQL statement
void sqldisconn(); // Free pointers to env, stat, conn,
// and disconnect
void error_out(); // Displays errors
};
int WINAPI WinMain (HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
// Declare an instance of the direxec object.
direxec x;
// Allocate handles, and connect.
x.sqlconn();
&n bsp; // Execute SQL command "SELECT first name, last_name FROM
employee".
x.sqlexec((UCHAR FAR *)"SELECT first name, last_name FROM employee");
// Free handles, and disconnect.
x.sqldisconn();
// Return success code; example executed successfully.
return (TRUE);
}
// Constructor initializes the string chr_ds_name with the
// data source name.
direxec::direxec()
{
_mbscpy(chr_ds_name,(const unsigned char *)"Northwind");
}
// Allocate environment handle, allocate connection handle,
// connect to data source, and allocate statement handle.
void direxec::sqlconn(void)
{
SQLAllocEnv(&henv);
SQLAllocConnect(henv,&hdbc);
rc=SQLConnect(hdbc,chr_ds_name,SQL_NTS,NULL,0,NULL,0);
// Deallocate handles, display error message, and exit.
if (!MYSQLSUCCESS(rc))
{
SQLFreeEnv(henv);
SQLFreeConnect(hdbc);
error_out();
exit(-1);
}
rc=SQLAllocStmt(hdbc,&hstmt);
}
/ / Execute SQL command with SQLExecDirect() ODBC API.
void direxec::sqlexec(unsigned char * cmdstr)
{
rc=SQLExecDirect(hstmt,cmdstr,SQL_NTS);
if (!MYSQLSUCCESS(rc)) //Error
{
error_out();
// Deallocate handles and disconnect.
; SQLFreeStmt(hstmt,SQL_DROP);
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
exit(-1);
}
else
{
for (rc=SQLFetch(hstmt); rc == S QL_SUCCESS; rc=SQLFetch(hstmt))
{
SQLGetData(hstmt,1,SQL_C_CHAR,szData,sizeof(szData),&cbData);
// In this example, the data is returned in a messagebox
// for simplicity. However, normally the SQLBindCol() ODBC API
// could be called to bind individual rows of data and assign
// for a rowset.
MessageBox(NULL,(const char *)szData,"ODBC",MB_OK);
}
}
}
// Free the statement handle, disconnect, free the connection handle,
and
// free the environment handle.
void direxec::sqldisconn(void)
{
SQLFreeStmt(hstmt,SQL_DROP);
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
}
// Display error message in a message box that has an OK button.
void direxec::error_out(void)
{
unsigned char szSQLSTATE[10];
SDWORD nErr;
unsigned char msg[SQL_MAX_MESSAGE_LENGTH+1];
SWORD cbmsg;
while(SQLError(0,0,hstmt,szSQLSTATE,&nErr,msg,sizeof(msg),&cbmsg)==
SQL_SUCCESS)
{
wsprintf((char *)szData,"Error:\nSQLSTATE=%s, Native error=%ld,
msg='%s'",szSQLSTATE,nErr,msg);
MessageBox(NULL,(const char *)szData,"ODBC Error",MB_OK);
}
}
When I try to build this project (It's "Windows Application Project",
defined in Dev-C++) with , I recive this:
Compiler: Default compiler
Building Makefile: "D:\Documents\to0745\Workspace\Makefile.win"
Executing make clean
rm -f main.o Project5.exe
< span style="color: rgb(0, 51, 255);">g++.exe -c main.cpp -o main.o
-I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"
-I"C:/Dev-Cpp/include/c++/3.4.2/backward"
-I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"
-I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
g++.exe main.o -o "Project5.exe" -L"C:/Dev-Cpp/lib" -mwindows
odbc32.lib !
C:/Dev-Cpp/lib/libmingw32.a(main.o)(.text+0x106):main.c: undefined
reference to `WinMain@16'
collect2: ld returned 1 exit status
make.exe: *** [Project5.exe] Error 1
Execution terminated
I searched Internet through the Google, but every issue concerning on
"undefined reference to WinMain@16", was just a mistake in confusing
main and WinMain. Here I'm adding Makefile:
# Project: Project5
# Makefile created by Dev-C++ 4.9.9.2
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
RES =
OBJ = main.o $(RES)
LINKOBJ = main.o $(RES)
LIBS = -L"C:/Dev-Cpp/lib" -mwindows odbc32.lib
INCS = -I"C:/Dev-Cpp/include"
CXXINCS = -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"
-I"C:/Dev-Cpp/include/c++/3.4.2/backward"
-I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"
-I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
BIN = Project5.exe
CXXFLAGS = $(CXXINCS)
CFLAGS = $(INCS)
RM = rm -f
.PHONY: all all-before all-after clean clean-custom
all: all-before Project5.exe all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o "Project5.exe" $(LIBS)
main.o: main.cpp
$(CPP) -c main.cpp -o main.o $(CXXFLAGS)
Thanks you very much for your advices...
jcd
In dev c++ u should go to the project options. Under the <parameters>
tab look 4 the LINKER textbox and write as follows: -lodbc32
|