PDA

View Full Version : newbie Q: why doesn't this build in Xcode?


stuudent
08-04-2007, 06:59 PM
The code below fails utterly in Xcode. Obviously, I'm new to Xcode. :D

Have checked all the build options. Turned off ZeroLink. Set paths for postgresql82 header and library. Set custom linker option -lpq.

// main.c
#include <libpq-fe.h>
int main (int argc, char * const argv[]) {
PGconn *conn = PQconnectdb(NULL);
PQfinish(conn);
return 0;
}

But same file compiles and links fine in gcc!
gcc -Wall -I/opt/local/include/postgresql82 -L/opt/local/lib/postgresql82 main.c -lpq -o hello

What gives?

-- stuudent

hayne
08-04-2007, 09:23 PM
You neglected to tell us exactly what you mean by "fails".
Doesn't compile? Doesn't link? Doesn't run? Doesn't do what you expect?

Have you looked at the command-line being used by Xcode to compare it to your 'gcc' command?
To see the command being used by Xcode, click the little icon at the bottom of the Build window that looks like a small page of text.
It's the 3rd from the left in the screen capture below.

stuudent
08-05-2007, 03:20 AM
Hayne, thank you. The code had compiled but failed to link.

Your hint to look at the Xcode generated command-line calls is great! I love it! Here's the relevant output if you are interested to help me further troubleshoot this problem:

**************
Ld /Users/test/src/+Xcode/+bin/Debug/HelloC normal i386
cd /Users/test/src/+Xcode/HelloC
/usr/bin/gcc-4.0 -o /Users/test/src/+Xcode/+bin/Debug/HelloC -L/Users/test/src/+Xcode/+bin/Debug -L/opt/local/lib/mysql5/mysql -L/opt/local/lib/postgresql82 -F/Users/test/src/+Xcode/+bin/Debug -filelist /Users/test/src/+Xcode/+obj/HelloC.build/Debug/HelloC.build/Objects-normal/i386/HelloC.LinkFileList -arch i386 -Wl,-Y,1455 -mmacosx-version-min=10.4 -lmysqlclient -lsqlite3 -lpq -isysroot /Developer/SDKs/MacOSX10.4u.sdk

/usr/bin/ld: warning can't open dynamic library: /Developer/SDKs/MacOSX10.4u.sdk/opt/local/lib/libssl.0.9.8.dylib referenced from: /opt/local/lib/mysql5/mysql/libmysqlclient.dylib (checking for undefined symbols may be affected) (No such file or directory, errno = 2)

/usr/bin/ld: warning can't open dynamic library: /Developer/SDKs/MacOSX10.4u.sdk/opt/local/lib/libcrypto.0.9.8.dylib referenced from: /opt/local/lib/mysql5/mysql/libmysqlclient.dylib (checking for undefined symbols may be affected) (No such file or directory, errno = 2)

/usr/bin/ld: warning can't open dynamic library: /Developer/SDKs/MacOSX10.4u.sdk/opt/local/lib/libz.1.dylib referenced from: /opt/local/lib/mysql5/mysql/libmysqlclient.dylib (checking for undefined symbols may be affected) (No such file or directory, errno = 2)

/usr/bin/ld: Undefined symbols: ...list of many undefined symbols...
**************

Duh. There are no files listed in the path "/Developer/SDKs/MacOSX10.4u.sdk/opt/local/lib/". How did this path get generated?

On a hunch, I cleared the build setting for "SDK Path" which is set to "/Developer/SDKs/MacOSX10.4u.sdk". Xcode could build properly.

But isn't the SDK needed for building Objective-C Cocoa apps? So it seems this isn't a final solution, because I'm trying to connect to PostgreSQL, MySQL, and SQLite through their C API from Cocoa.

Thinking maybe because of linking to dynamic library that Xcode has to guess where the files are located? So what if the postgresql library is linked statically?

Hmmm... how to tell Xcode to link to a static library instead of the dynamic library. There's a build option for the standard library. But I don't see anything obvious for non-standard libraries.

Got any hints?

-- stuudent

stuudent
08-07-2007, 05:53 PM
FWIW:

I found an easier method than outlined previously for solving the linking problem described originally. I created a link to /opt under /Developer/SDKs/MacOSX10.4u.sdk. Now don't have to worry about it for future projects.