GNUstep and SQLite3

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

GNUstep and SQLite3

Andreas Höschler
Hi all,

I would like to access a SQLite database with a minimalistic Objective-C (GNUstep) program. I installed SQLite like so

tar xvf sqlite-autoconf-3230100.tar
cd sqlite-autoconf-3230100
./configure
make
make install

and got

/usr/local/bin/sqlite3

/usr/include/php/ext/sqlite3/libsqlite
/usr/lib/libsqlite3.0.dylib
/usr/lib/libsqlite3.dylib
/usr/local/lib/libsqlite3.0.dylib
/usr/local/lib/libsqlite3.a
/usr/local/lib/libsqlite3.dylib
/usr/local/lib/libsqlite3.la

I have now written the following Objective-C test program but don't get it linked! :-(

***************************************************************************************************
#import <Foundation/Foundation.h>
#include <sqlite3.h>

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
  {
   int i;
   for(i=0; i<argc; i++)
     {
      printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
     }
   printf("\n");
   return 0;
  }
 
int main (int argc, const char *argv[])
  {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   // insert your code here
     {
      sqlite3 *db;
      int rc;
      
      rc = sqlite3_open("/Users/ahoesch/Development/SQLite/sqlite-autoconf-3230100/ex1", &db);
      if (rc) 
        {
         NSLog(@"Can't open database: %s\n", sqlite3_errmsg(db));
         sqlite3_close(db);
         return(1);
        }
      
      char *zErrMsg = 0;
      rc = sqlite3_exec(db, "select * from PERSON;", callback, 0, &zErrMsg);
        if (rc != SQLITE_OK)
          {
           fprintf(stderr, "SQL error: %s\n", zErrMsg);
           sqlite3_free(zErrMsg);           
          }
        sqlite3_close(db);
     }
   
   [pool release];
   exit(0);       // insure the process exit status is 0
   return 0;      // ...and make main fit the ANSI spec.
}
***************************************************************************************************
With the GNumakefile



I get 

admins-iMac:FirstAccess ahoesch$ make clean
This is gnustep-make 2.4.0. Type 'make print-gnustep-make-help' for help.
rm -rf /Build/FirstAccess/*~ /Build/FirstAccess/obj
admins-iMac:FirstAccess ahoesch$ make
This is gnustep-make 2.4.0. Type 'make print-gnustep-make-help' for help.
Making all for tool FirstAccess...
 Compiling file main.m ...
 Linking tool FirstAccess ...
Undefined symbols for architecture i386:
  "_sqlite3_close", referenced from:
      _main in main.m.o
  "_sqlite3_errmsg", referenced from:
      _main in main.m.o
  "_sqlite3_exec", referenced from:
      _main in main.m.o
  "_sqlite3_free", referenced from:
      _main in main.m.o
  "_sqlite3_open", referenced from:
      _main in main.m.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [/Build/FirstAccess/obj/FirstAccess] Error 1
make[2]: *** [internal-tool-all_] Error 2
make[1]: *** [FirstAccess.all.tool.variables] Error 2
make: *** [internal-all] Error 2

That's probably because sqlite lib was built for for x86_64 and cannot be mixed with 32 bit code!? I extracted the gcc calls with make messages=yes and remove all -m32 occurrences. This now gives me


admins-iMac:FirstAccess ahoesch$ cd /Build/FirstAccess; /Library/GNUstep/Makefiles/mkinstalldirs ./obj
admins-iMac:FirstAccess ahoesch$ cd /Build/FirstAccess; /Library/GNUstep/Makefiles/mkinstalldirs ./obj/FirstAccess.obj/
admins-iMac:FirstAccess ahoesch$ cd /Users/ahoesch/Development/SQLite/FirstAccess
admins-iMac:FirstAccess ahoesch$ gcc main.m -c -MMD -MP -DNeXT_Foundation_LIBRARY=1 -DNeXT_GUI_LIBRARY=1 -DNeXT_RUNTIME=1 -dynamic -fno-common -Wall -DGSWARN -DGSDIAGNOSE -Wno-import -g -O2 -fno-strict-aliasing -fnext-runtime -Wno-parentheses -Wno-import -I. -I/usr/local/include/ -F/Library/Frameworks/ -o /Build/FirstAccess/obj/FirstAccess.obj/main.m.o
admins-iMac:FirstAccess ahoesch$ gcc -fnext-runtime -o /Build/FirstAccess/obj/FirstAccess /Build/FirstAccess/obj/FirstAccess.obj/main.m.o -ldl -lpthread -lz -lm   -L/usr/local/lib/ -F/Library/Frameworks/    -framework Foundation       -lm
Undefined symbols for architecture x86_64:
  "_sqlite3_close", referenced from:
      _main in main.m.o
  "_sqlite3_errmsg", referenced from:
      _main in main.m.o
  "_sqlite3_exec", referenced from:
      _main in main.m.o
  "_sqlite3_free", referenced from:
      _main in main.m.o
  "_sqlite3_open", referenced from:
      _main in main.m.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am stuck! Any idea whatI am doing wrong and how to get this working?

Thanks a lot,

 Andreas



_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep

GNUmakefile (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: GNUstep and SQLite3

Andreas Höschler
Additional Info: I was doing this on MacOSX. I will try it on Ubuntu now but probably will see the same result!?

Thanks,

 Andreas



_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep
Reply | Threaded
Open this post in threaded view
|

Re: GNUstep and SQLite3

Ivan Vučica-2
In reply to this post by Andreas Höschler
This is on a Mac, judging on the hostname and /Users/ being parent of the homedir. Therefore I will guess that GNUstep is built as a universal binary and the test program is being built as a universal ("fat") binary.

At the very least, force the test program to not be built as a universal binary.

Better is to build sqlite3 as universal.

Best is to use Cocoa if you have it available – in my personal opinion. This is best unless you depend on gnustep-make or other GNUstep specific features. And use GNUstep on a platform where it won't conflict with Cocoa.

On June 5, 2018 2:48:21 PM GMT+01:00, "Andreas Höschler" <[hidden email]> wrote:
Hi all,

I would like to access a SQLite database with a minimalistic Objective-C (GNUstep) program. I installed SQLite like so

tar xvf sqlite-autoconf-3230100.tar
cd sqlite-autoconf-3230100
./configure
make
make install

and got

/usr/local/bin/sqlite3

/usr/include/php/ext/sqlite3/libsqlite
/usr/lib/libsqlite3.0.dylib
/usr/lib/libsqlite3.dylib
/usr/local/lib/libsqlite3.0.dylib
/usr/local/lib/libsqlite3.a
/usr/local/lib/libsqlite3.dylib
/usr/local/lib/libsqlite3.la

I have now written the following Objective-C test program but don't get it linked! :-(

***************************************************************************************************
#import <Foundation/Foundation.h>
#include <sqlite3.h>

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
  {
   int i;
   for(i=0; i<argc; i++)
     {
      printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
     }
   printf("\n");
   return 0;
  }
 
int main (int argc, const char *argv[])
  {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   // insert your code here
     {
      sqlite3 *db;
      int rc;
      
      rc = sqlite3_open("/Users/ahoesch/Development/SQLite/sqlite-autoconf-3230100/ex1", &db);
      if (rc) 
        {
         NSLog(@"Can't open database: %s\n", sqlite3_errmsg(db));
         sqlite3_close(db);
         return(1);
        }
      
      char *zErrMsg = 0;
      rc = sqlite3_exec(db, "select * from PERSON;", callback, 0, &zErrMsg);
        if (rc != SQLITE_OK)
          {
           fprintf(stderr, "SQL error: %s\n", zErrMsg);
           sqlite3_free(zErrMsg);           
          }
        sqlite3_close(db);
     }
   
   [pool release];
   exit(0);       // insure the process exit status is 0
   return 0;      // ...and make main fit the ANSI spec.
}
***************************************************************************************************
With the GNumakefile


--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep
Reply | Threaded
Open this post in threaded view
|

Re: GNUstep and SQLite3

Ivan Vučica
In reply to this post by Andreas Höschler
Ubuntu doesn't do fat binaries, and it's less trivial to change archs, so you won't.

On June 5, 2018 2:55:10 PM GMT+01:00, "Andreas Höschler" <[hidden email]> wrote:
Additional Info: I was doing this on MacOSX. I will try it on Ubuntu now but probably will see the same result!?

Thanks,

Andreas





Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep

_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep
Reply | Threaded
Open this post in threaded view
|

Re: GNUstep and SQLite3

Wolfgang Lux
In reply to this post by Andreas Höschler

> Am 05.06.2018 um 14:48 schrieb Andreas Höschler <[hidden email]>:
>
> I am stuck! Any idea whatI am doing wrong and how to get this working?

Add -lsqlite3 to FirstAccess_TOOL_LIBS. :-)
BTW, you should also add the other -l options to FirstAccess_TOOL_LIBS and not to _LIB_DIRS, which is intended to be used for -L options. This has a better chance of getting the order of flags right on systems where it matters.

Wolfgang
_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep
Reply | Threaded
Open this post in threaded view
|

Re: GNUstep and SQLite3

Andreas Höschler
In reply to this post by Ivan Vučica-2
Hi Ivan,

This is on a Mac, judging on the hostname and /Users/ being parent of the homedir.

Yes! However, I use GNUstep make to build all my stuff even on MacOSX, so no xCode anywhere.

Therefore I will guess that GNUstep is built as a universal binary and the test program is being built as a universal ("fat") binary.

I haven't built GNUstep on MacOSX but am using Cocoa (Foundation, AppKit). I just use GNUstep make to build my stuff. 

At the very least, force the test program to not be built as a universal binary.

How do I do that with GNUstep make? What do I need to change in my GNUmakefile?




Better is to build sqlite3 as universal.

Well, I used the recommended

tar xvf sqlite-autoconf-3230100.tar
cd sqlite-autoconf-3230100
./configure
make
make install

to build it and have no idea what to do else to build it universal!? :-(

Thanks,

 Andreas


_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep

GNUmakefile (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: GNUstep and SQLite3

Ivan Vučica-2
In reply to this post by Wolfgang Lux

> On 5 Jun 2018, at 18:39, Wolfgang Lux <[hidden email]> wrote:
>
>
>> Am 05.06.2018 um 14:48 schrieb Andreas Höschler <[hidden email]>:
>>
>> I am stuck! Any idea whatI am doing wrong and how to get this working?
>
> Add -lsqlite3 to FirstAccess_TOOL_LIBS. :-)
> BTW, you should also add the other -l options to FirstAccess_TOOL_LIBS and not to _LIB_DIRS, which is intended to be used for -L options. This has a better chance of getting the order of flags right on systems where it matters.

This doesn’t fully explain the latter attempt when manually invoking gcc (which is probably clang).
_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep
Reply | Threaded
Open this post in threaded view
|

Re: GNUstep and SQLite3

Ivan Vučica-2
In reply to this post by Andreas Höschler


On 5 Jun 2018, at 18:40, Andreas Höschler <[hidden email]> wrote:

Hi Ivan,

This is on a Mac, judging on the hostname and /Users/ being parent of the homedir.

Yes! However, I use GNUstep make to build all my stuff even on MacOSX, so no xCode anywhere.

Therefore I will guess that GNUstep is built as a universal binary and the test program is being built as a universal ("fat") binary.

I haven't built GNUstep on MacOSX but am using Cocoa (Foundation, AppKit). I just use GNUstep make to build my stuff. 

At the very least, force the test program to not be built as a universal binary.

How do I do that with GNUstep make? What do I need to change in my GNUmakefile?

Your problem was also present when invoking gcc manually. I expected you’d first try that.

You compiled and linked as follows:

admins-iMac:FirstAccess ahoesch$ gcc main.m -c -MMD -MP -DNeXT_Foundation_LIBRARY=1 -DNeXT_GUI_LIBRARY=1 -DNeXT_RUNTIME=1 -dynamic -fno-common -Wall -DGSWARN -DGSDIAGNOSE -Wno-import -g -O2 -fno-strict-aliasing -fnext-runtime -Wno-parentheses -Wno-import -I. -I/usr/local/include/ -F/Library/Frameworks/ -o /Build/FirstAccess/obj/FirstAccess.obj/main.m.o
admins-iMac:FirstAccess ahoesch$ gcc -fnext-runtime -o /Build/FirstAccess/obj/FirstAccess /Build/FirstAccess/obj/FirstAccess.obj/main.m.o -ldl -lpthread -lz -lm   -L/usr/local/lib/ -F/Library/Frameworks/    -framework Foundation       -lm

You could add -m32 or -m64 here as well. Or, Mac-style, -arch i386 or -arch x86_64. This depends on what your SQLite was built with.



<GNUmakefile>

To complement Wolfgang’s suggestion, set the following:
- FirstAccess_CFLAGS
- FirstAccess_OBJCFLAGS
- FirstAccess_LDFLAGS




Better is to build sqlite3 as universal.

Well, I used the recommended

tar xvf sqlite-autoconf-3230100.tar
cd sqlite-autoconf-3230100
./configure
make
make install

to build it and have no idea what to do else to build it universal!? :-(

If I recall correctly (I probably last did this stuff ~2011, or 2013 at the latest), pass both "-arch i386” and "-arch x86_64” flags to both compiler and linker (i.e. add them to CFLAGS, CXXFLAGS, LDFLAGS when invoking configure).

_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep
Reply | Threaded
Open this post in threaded view
|

Re: GNUstep and SQLite3

Wolfgang Lux
In reply to this post by Ivan Vučica-2


> Am 05.06.2018 um 19:33 schrieb Ivan Vučica <[hidden email]>:
>
>
>> On 5 Jun 2018, at 18:39, Wolfgang Lux <[hidden email]> wrote:
>>
>>
>>> Am 05.06.2018 um 14:48 schrieb Andreas Höschler <[hidden email]>:
>>>
>>> I am stuck! Any idea whatI am doing wrong and how to get this working?
>>
>> Add -lsqlite3 to FirstAccess_TOOL_LIBS. :-)
>> BTW, you should also add the other -l options to FirstAccess_TOOL_LIBS and not to _LIB_DIRS, which is intended to be used for -L options. This has a better chance of getting the order of flags right on systems where it matters.
>
> This doesn’t fully explain the latter attempt when manually invoking gcc (which is probably clang).

I'm not exactly sure what your comment is referring to. The problem is that there is no -lsqlite3 option to link with the SQLite3 library in the first place and that fully explains the undefined _sqlite3_* symbols during the link step.
My additional advice on the use of the TOOL_LIBS and LIB_DIRS flags is just for getting the order of options right (otherwise he'd might link with the SQLite3 version that comes with macOS and is installed in /usr/lib rather than the one installed locally in /usr/local/lib, although I doubt it will make much of a difference in this particular case).

Wolfgang
_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep
Reply | Threaded
Open this post in threaded view
|

Re: GNUstep and SQLite3

Ivan Vučica-2
On Tue 5 Jun 2018 at 21:00 Wolfgang Lux <[hidden email]> wrote:


> Am 05.06.2018 um 19:33 schrieb Ivan Vučica <[hidden email]>:
>
>
>> On 5 Jun 2018, at 18:39, Wolfgang Lux <[hidden email]> wrote:
>>
>>
>>> Am 05.06.2018 um 14:48 schrieb Andreas Höschler <[hidden email]>:
>>>
>>> I am stuck! Any idea whatI am doing wrong and how to get this working?
>>
>> Add -lsqlite3 to FirstAccess_TOOL_LIBS. :-)
>> BTW, you should also add the other -l options to FirstAccess_TOOL_LIBS and not to _LIB_DIRS, which is intended to be used for -L options. This has a better chance of getting the order of flags right on systems where it matters.
>
> This doesn’t fully explain the latter attempt when manually invoking gcc (which is probably clang).

I'm not exactly sure what your comment is referring to. The problem is that there is no -lsqlite3 option to link with the SQLite3 library in the first place and that fully explains the undefined _sqlite3_* symbols during the link step.

I did not notice that. That would indeed explain the entire problem away.

I mistakenly thought that the issue is in archs, so I was commenting that the issue would not appear in the manual invocation sans-gnustep-make.

Since there is no -lsqlite3, that explains everything.


My additional advice on the use of the TOOL_LIBS and LIB_DIRS flags is just for getting the order of options right (otherwise he'd might link with the SQLite3 version that comes with macOS and is installed in /usr/lib rather than the one installed locally in /usr/local/lib, although I doubt it will make much of a difference in this particular case).

Wolfgang
--
Sent from Gmail Mobile

_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep
Reply | Threaded
Open this post in threaded view
|

Re: GNUstep and SQLite3

Andreas Höschler
In reply to this post by Wolfgang Lux
Hi Ivan and Wolfgnag,

thanks for all the given hints and background info (compiler and linker flags)

Am 05.06.2018 um 14:48 schrieb Andreas Höschler <[hidden email]>:

I am stuck! Any idea whatI am doing wrong and how to get this working?

Add -lsqlite3 to FirstAccess_TOOL_LIBS. :-)

This fixed the issue. I could build the test app with this option and successfully run it.

Thanks a lot,

 Andreas


_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep