0

On OpenSuse 11.2, I successfully compiled, linked, and ran the following code which installs a data source for a MySQL database with unixODBC:

#include <iostream>
#include <sql.h>
#include <sqlext.h>
#include <odbcinst.h>

/* Add a data source for the following MySQL db: db=testdb, username=test, password = test. */
void inst()
{
   BOOL ret = SQLConfigDataSource(NULL, ODBC_ADD_DSN, "MySQL driver",
                                  "DSN=mysource\0UID=test\0PWD=test\0DATABASE=testdb\0\0");
   if (!ret) {
      DWORD errCode;
      char errBuf[SQL_MAX_MESSAGE_LENGTH];
      WORD msgLen;
      SQLInstallerError(1, &errCode, errBuf, SQL_MAX_MESSAGE_LENGTH, &msgLen);
      std::cerr << errBuf << std::endl;
   }
}

int main()
{
     inst();
     return 0;
}

With the same code on Debian Lenny, I have had problems. First, I compiled this code the following way:

c++ -o main main.cc  -lodbc -lodbcinst -L/usr/lib/odbc -lmyodbc

It went ok. But when I attempted to run the resulting binary, I got a linker error which in fact was confirmed by typing ldd main:

libmyodbc3_r-3.51.15.so => not found

Although I correctly installed unixODBC and the associated MySQL driver (myodbc) on my host (Debian Lenny) the simplest way (i.e. via aptitude), I could not find this shared library.

I wrongly thought, well, I will create a symlink on /usr/lib/odbc/libmyodbc.so. Anyway now my program returns the following message:

General installer error

So I feel the file libmyodbc3_r-3.51.15.so is really missing.

Note: on Debian Lenny, the version of unixODBC is 2.2.11, and the version of MySQL is 5.0.51a

Anyone ever ran into such a situation ? Any help would be appreciated.

alex_msk
  • 1
  • 2

1 Answers1

0

The option

-L/usr/lib/odbc

tells the compiler where to find the library for linking.

But the system doesn't know where to find the library when you run the executable.

You need to either statically link against libmyodbc, or tell the system where to find the library.

The first can be done by changing

-lmyodbc

to

-static -lmyodbc

The second can be done by editing /etc/ld.so.conf (or adding to /etc/ld.so.conf.d) and re-running ldconfig or by setting the LD_LIBRARY_PATH environment variable to include /usr/lib/odbc

Bill B
  • 1,280
  • 13
  • 16
  • A third option is to hardcode the /usr/lib/odbc path in the binary, do that by adding the flag `-Wl,-rpath,/usr/lib/odbc` – nos Feb 28 '11 at 20:49