CDT Group:
I've tried 1) finding a solution to this on the web (FAQ included) to no
avail, 2) have try numerous combinations of settings in eclipse myself,
and 3) really need for this to work, so I apologize in advance for this
seemingly simple question, which is this:
How do I link in external libraries using CDT?
I'm using the version posted at http://www.eclipse.org/cdt/downloads.php
as of yesterday.
I'm porting code that needs to be linked against the odbc library, using
the following example from the original (non-eclipse) makefile:
## these are general compiler directives
CC = gcc
LIB = /usr/lib/libiodbc.a
CFLAGS = -g -Wall -Iinclude:/usr/include -D_DEBUG
FFLAGS = -O
time2fly: ${OBJS} ${LIB}
${CC} -o $@ ${OBJS} ${LIB} -framework CoreFoundation
and get the following results, when I try to link using the MacOS X C
Linker:
Building target: time2fly
Invoking: MacOS X C Linker
gcc -L/usr/lib -o "time2fly" ./main.o -llibiodbc.a
ld: library not found for -llibiodbc.a
collect2: ld returned 1 exit status
make: *** [time2fly] Error 1
and the following, when I try to link using the GNU C Linker, using the
following settings:
C/C++ Build->Settings->Tool Settings->Libraries: libiodbc.a
C/C++ Build->Settings->Tool Settings->Library search path: /usr/lib
Building target: time2fly
Invoking: GCC C Linker
gcc -L/usr/lib -o"time2fly" ./main.o -llibiodbc.a
ld: unknown option: -otime2fly
collect2: ld returned 1 exit status
make: *** [time2fly] Error 1
Which is very interest to me since for the OS X C Linker, the error
reported is that the library is not found, and for the GCC C Linker, the
error is an unknown option which if I recall, renames the output, yes?
No mention of not finding the external library is found. I've included
the source below, which builds in XCode (but the dubugging environment
isn't "noteworthy"), my old command line make (parts above) and eclipse
seems to have made great strides for C/C++ development. I really want
to use eclipse, since XCode just can't hack it, there's no dev-studio on
OSX and it's time 2 fly... can anyone please help?
/* main.c */
#include <math.h>
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcinst.h>
void print_odbc_error(
FILE *fp,
SQLSMALLINT handle_type,
SQLHANDLE hndl );
void open_odbc_connection(
unsigned long *return_code,
const char *data_source_name,
SQLHENV *henv,
SQLHDBC *hdbc,
FILE *fp );
void close_odbc_connnection(
SQLHENV *henv,
SQLHDBC *hdbc );
int main( int argc, char *argv[] )
{
unsigned long return_code = 0;
double dad[1000][30];
/* odbc connection variables (handles) */
SQLHENV henv; /* handle the environment */
SQLHDBC hdbc; /* handle for the connection */
memset( dad, 0, sizeof( dad ) );
dad[301][29] = 4.0 * atan( 1.0 );
/* TODO: make sure you don't screw this up... */
/* the debugger shows 2 different values for this element */
/* see attached pictures */
fprintf( stdout, "hi mom..., %lf\n", dad[301][29] );
/* obtain a connection to the odbc data source */
/* for now, keep the high level error handling to a minimum */
open_odbc_connection( &return_code, "moroi", &henv, &hdbc, NULL );
if( return_code )
{
return 0;
}
close_odbc_connnection( &henv, &hdbc );
return 0;
}
void print_odbc_error(
FILE *fp,
SQLSMALLINT handle_type,
SQLHANDLE hndl )
{
SQLRETURN result;
SQLCHAR sql_state[6];
SQLINTEGER native_error;
SQLSMALLINT record_number;
SQLSMALLINT required_length;
SQLCHAR message_text[SQL_MAX_MESSAGE_LENGTH+1];
record_number = 1;
result = SQL_SUCCESS;
while( result == SQL_SUCCESS )
{
#ifdef _DEBUG
syslog( LOG_NOTICE, "before SQLGetDiagRec()" );
#endif
result = SQLGetDiagRec( handle_type,
hndl,
record_number,
sql_state,
&native_error,
message_text,
sizeof(message_text),
&required_length );
#ifdef _DEBUG
syslog( LOG_NOTICE, "after SQLGetDiagRec()" );
#endif
if( result == SQL_SUCCESS )
{
syslog( LOG_NOTICE, "{rufusd.moroi] ODBC Warning/Error" );
syslog( LOG_NOTICE, "SQLState = %s\n", sql_state );
syslog( LOG_NOTICE, "Native Error = %d\n", native_error );
syslog( LOG_NOTICE, "Message Text = %s\n", message_text );
record_number++ ;
}
}
}
void open_odbc_connection(
unsigned long *return_code,
const char *data_source_name,
SQLHENV *henv,
SQLHDBC *hdbc,
FILE *debug_file )
{
SQLRETURN retcode;
/* Allocate environment handle */
retcode = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, henv );
/* make sure you take note of the type of handle you're working
with */
/* when passing to the error handling
function */
/* the function's behaviour will depend on
it */
if( retcode == SQL_ERROR )
{
print_odbc_error( debug_file, SQL_HANDLE_ENV, henv );
*return_code = 1;
return;
}
/* Set the ODBC version environment attribute */
retcode = SQLSetEnvAttr( *henv,
SQL_ATTR_ODBC_VERSION,
(void*)SQL_OV_ODBC3,
0 );
if( retcode == SQL_ERROR )
{
print_odbc_error( debug_file, SQL_HANDLE_ENV, henv );
*return_code = 1;
return;
}
/* Allocate connection handle */
retcode = SQLAllocHandle( SQL_HANDLE_DBC, *henv, hdbc );
if( retcode == SQL_ERROR )
{
print_odbc_error( debug_file, SQL_HANDLE_DBC, *hdbc );
*return_code = 1;
return;
}
/* Connect to data source */
retcode = SQLConnect( *hdbc,
(SQLCHAR*)data_source_name,
SQL_NTS,
(SQLCHAR*)"postgres",
SQL_NTS,
(SQLCHAR*)"",
SQL_NTS);
if( retcode == SQL_ERROR )
{
print_odbc_error( debug_file, SQL_HANDLE_DBC, *hdbc );
*return_code = 1;
return;
}
*return_code = 0;
}
void close_odbc_connnection(
SQLHENV *henv,
SQLHDBC *hdbc )
{
/* if the environment handle exists */
/* check to see if the connection handle exists */
if( henv )
{
/* if so, disconnect */
if( hdbc )
{
SQLDisconnect( *hdbc );
SQLFreeHandle( SQL_HANDLE_DBC, hdbc );
}
/* free the environment handle */
SQLFreeEnv( henv );
}
}
Thanks,
Jeff D. Hamann, PhD
Forest Informatics, Inc.
_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/cdt-dev