MINGW + SQLITE3 - DLL / Linker Issues - c

Trying to call SQLITE3 functions in MINGW C - program does not behave as expected. I suspect the issue is around the .a file but cannot see where or why there is no output.
Installed latest MinGW Distro from nuwen.net on Windows 10 in c:\mingw.
C:\c>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../src/configure --enable-languages=c,c++ --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --disable-multilib --prefix=/c/temp/gcc/dest --with-sysroot=/c/temp/gcc/dest --disable-libstdcxx-pch --disable-libstdcxx-verbose --disable-nls --disable-shared --disable-win32-registry --with-tune=haswell --enable-threads=posix --enable-libgomp
Thread model: posix
gcc version 8.1.0 (GCC)
Downloaded sqlite-amalgamation-3260000.zip, sqlite-dll-win64-x64-3260000.zip and sqlite-tools-win32-x86-3260000.zip from sqlite.org.
The headers sqlite3.h and sqlite3ext.h were copied from sqlite-amalgamation-3260000.zip to the include\sqlite3 directory.
The files sqlite3.def and sqlite3.dll were extracted from sqlite-dll-win64-x64-3260000.zip to a temporary directory.
In the temporary directory, I ran dlltool -U -d sqlite3.def -l libsqlite3.a (as described in http://www.mingw.org/wiki/createimportlibraries).
(note I tried with and without -U and -k flags)
No messages were output and the libsqlite3.a file is approx 200K (although I note .DLL is 10x size). I copied the resultant libsqlite3.a to the lib directory.
I write a quick test3.c program just to check I can call an sqlite function correctly:
#include <stdio.h>
#include "sqlite3\sqlite3.h"
sqlite3* db;
int main() {
printf("Start\n");
sqlite3_open("test.db", &db);
if(db == 0) {
printf("Could not open database.\n");
}
else {
printf("Database Open \n");
}
}
I compile with no errors using: gcc test3.c -lsqlite3
I run a.exe and see absolutely nothing - not even the "Start" string printed
I comment out just the "sqlite3_open" line and recompile with same command then run again and I see
C:\c>a
Start
Could not open database.
As behaviour is unexpected when calling an sqlite3 function, and as the .a file is a tenth of the size of the .DLL, I suspect the problem is there - but still cannot see why the "Start" is not output.

Related

Undefined refrences to '__imp' and '__cxa' when linking libraw

I'm trying to link the c++ library, libraw, into my c program.
windows 10 msys2. Using clion, but also trying to compile from mingw64 terminal using gcc and clang
At first, I was using the version of libraw that i got from msys2 pacman. The issue is that the version in the repo is the latest release, but it is quite old and I need the beta release of libraw to support one of my newer cameras.
I've built the libraries from source (https://github.com/LibRaw/LibRaw) in mingw, and moved the libraw.a file, and .h files into the mingw64/lib and /include folder respectively, just as they appeared when added using pacman.
The issue is that now when I compile, a long list of errors is spit out including things such as undefined references to '__cxa'
I'm wondering what is causing these compile errors and what I can do to fix them, so that I can use libraw in my project. I assume it has something to do with how I'm compiling libraw, since using the version from pacman, the exact same c and cmake code works perfectly.
Here a simplified version of my c program (note that libtiff is working just fine, can can be ignored from the samples):
#include <stdio.h>
#include <tiffio.h>
#include <libraw.h>
int main(void);
int main(void)
{
const char* libtiffVersion;
const char* librawVersion;
libtiffVersion = TIFFGetVersion();
if(libtiffVersion)
printf("%s\n", libtiffVersion);
else
printf("libtiff not found!\n");
librawVersion = libraw_version();
if(librawVersion)
printf("%s\n", librawVersion);
else
printf("libraw not found!\n");
return 0;
}
and my cmake (tried with various c standards from 90 to 23, although i don't think it mattres):
cmake_minimum_required(VERSION 3.23)
project(MyProject C)
set(CMAKE_C_STANDARD 90)
find_library(LIBRAW libraw)
find_library(LIBTIFF libtiff)
add_executable(MyProject main.c)
target_link_libraries(MyProject ${LIBTIFF} ${LIBRAW})
The linker command generated by cmake is:
cmd.exe /C "cd . && C:\msys64\mingw64\bin\gcc.exe -g CMakeFiles/MyProject.dir/main.c.obj -o MyProject.exe -Wl,--out-implib,libMyProject.dll.a -Wl,--major-image-version,0,--minor-image-version,0 C:/msys64/mingw64/lib/libtiff.dll.a C:/msys64/mingw64/lib/libraw.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
I read that what could be causing some of the issue, is that libraw is a c++ library (includes a c wrapper), while I'm trying to compile a c program, but trying to compile from the command line with g++, clang++, gcc with -lsupc++ with no luck. doing it this way gives me different errors, many of them including undefined reference to '__imp'
I've also tried copying the libraw.a file into my source directory compiling with a path to the .a file, with the no success.
I've tried using the binary release of the older version of libraw that I know was working, by copying the libraw.lib file to my source directory and changing my cmake file:
cmake_minimum_required(VERSION 3.23)
project(MyProject C)
set(CMAKE_C_STANDARD 90)
find_library(LIBTIFF libtiff)
add_executable(MyProject main.c)
target_link_libraries(MyProject ${LIBTIFF} ${PROJECT_SOURCE_DIR}/libraw.lib)
This time it compiles, but immediatly segfualts. Might have something to do with the binaries being built for windows using msvc while in using msys2/mingw64

Compiling c program with MinGW causes cc1.exe fatal error

I installed MinGW-w64 using win-builds.
It all went ok but then when I compile a file using
gcc -Wall -o prog.exe main.c
I get this error
cc1.exe: fatal error: Files/win-builds-1.5.0/include: No such file or directory
compilation terminated.
I have updated gcc path. I get the same error when I try to build on eclipse.
The location where win-builds put all the files is C:\Program Files\win-builds-1.5.0\ and the folder \include\ exists.
My guess is that cc1.exe is trying to access C:\Program Files\win-builds-1.5.0\include but, for some reason it is separating C:\Program of Files\win-builds-1.5.0\include.
How can I get the compiler to work?
EDIT:
Here is the code to be compiled
/*
* main.c
*
* Created on: 13/06/2018
* Author: haslima
*/
#include <stdio.h>
int main()
{
printf("Hello World");
return 1;
}
EDIT:
when I run gcc -v -c -o prog.exe main.c I get this output:
Reading specs from c:/program files/win-builds-1.5.0/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/specs
COLLECT_GCC=gcc
Target: x86_64-w64-mingw32
Configured with: ../gcc-4.8.3/configure --prefix=/opt/windows_64 --with-sysroot=/opt/windows_64 --libdir=/opt/windows_64/lib64 --mandir=/opt/windows_64/man --infodir=/opt/windows_64/info --enable-shared --disable-bootstrap --disable-multilib --enable-threads=posix --enable-languages=c,c++ --enable-checking=release --enable-libgomp --with-system-zlib --with-python-dir=/lib64/python2.7/site-packages --disable-libunwind-exceptions --enable-__cxa_atexit --enable-libssp --with-gnu-ld --verbose --enable-java-home --with-java-home=/opt/windows_64/lib64/jvm/jre --with-jvm-root-dir=/opt/windows_64/lib64/jvm --with-jvm-jar-dir=/opt/windows_64/lib64/jvm/jvm-exports --with-arch-directory=amd64 --with-antlr-jar='/home/adrien/projects/win-builds-1.5/slackware64-current/d/gcc/antlr-*.jar' --disable-java-awt --disable-gtktest --build=x86_64-slackware-linux --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32
Thread model: posix
gcc version 4.8.3 (GCC)
COLLECT_GCC_OPTIONS='-v' '-c' '-o' 'prog.exe' '-mtune=generic' '-march=x86-64'
c:/program files/win-builds-1.5.0/bin/../libexec/gcc/x86_64-w64-mingw32/4.8.3/cc1.exe -quiet -v -iprefix c:\program files\win-builds-1.5.0\bin\../lib64/gcc/x86_64-w64-mingw32/4.8.3/ -D_REENTRANT -IC:/Program Files/win-builds-1.5.0/include main.c -quiet -dumpbase main.c -mtune=generic -march=x86-64 -auxbase-strip prog.exe -version -o C:\Users\hasli\AppData\Local\Temp\ccnpAzB7.s
GNU C (GCC) version 4.8.3 (x86_64-w64-mingw32)
compiled by GNU C version 4.8.3, GMP version 5.1.3, MPFR version 3.1.2, MPC version 0.8.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "c:/program files/win-builds-1.5.0/lib64/gcc/../../lib64/gcc/x86_64-w64-mingw32/4.8.3/include"
ignoring nonexistent directory "/opt/windows_64/opt/windows_64/lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../include"
ignoring duplicate directory "c:/program files/win-builds-1.5.0/lib64/gcc/../../lib64/gcc/x86_64-w64-mingw32/4.8.3/include-fixed"
ignoring duplicate directory "c:/program files/win-builds-1.5.0/lib64/gcc/../../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/include"
ignoring nonexistent directory "/opt/windows_64/mingw/include"
ignoring nonexistent directory "C:/Program"
#include "..." search starts here:
#include <...> search starts here:
c:\program files\win-builds-1.5.0\bin\../lib64/gcc/x86_64-w64-mingw32/4.8.3/include
c:\program files\win-builds-1.5.0\bin\../lib64/gcc/x86_64-w64-mingw32/4.8.3/include-fixed
c:\program files\win-builds-1.5.0\bin\../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/include
End of search list.
cc1.exe: fatal error: Files/win-builds-1.5.0/include: No such file or directory
compilation terminated.
From [MinGW]: Getting Started (including bolds):
MinGW may have problems with paths containing spaces, and if not, usually other programs used with MinGW will experience problems with such paths. Thus, we strongly recommend that you do not install MinGW in any location with spaces in the path name reference. You should avoid installing into any directory or subdirectory having names like "Program Files" or "My Documents", etc.
So, the solution is pretty straightforward:
Uninstall your current version (might not be necessary, but there's no point keeping something broken)
Make sure to read all installation requirements / notes
Install it in a SPACE free dir
I should have thought of this sooner, as I don't install stuff in default dirs (e.g. I have MinGW installed in "f:\Install\pc064\MinGW\MinGW-W64\x86_64-8.1.0-posix-seh-rt_v6-rev0").

compiling the source into object files using gcc -c *.c

My OS is Windows 10. I'm using command prompt this time to compile.
according to the book that I am reading, to compile all the source files and make it an object files, (in the current directory) do it by typing this command:
gcc -c *.c
it says the operating system will replace *.c with all the C filenames
But why am I getting this error?
gcc: error: *.c: Invalid argument
gcc: fatal error: no input files
compilation terminated.
I'm in the right directory.
And when I compile my source files with their respective names, it's working properly. But the '*.c' is not working.
And the same error in linking the object files using '*.o'
Is that command not for windows OS? If not, what is for windows?
Newbie here.
On Unix systems and environments (like MSYS or Cygwin) , wildcards are expanded by the shell (depends), but in Windows CMD, wildcards are expanded and interpreted by the program that receives. This sounds strange, but cmd.exe does not support wildcard expansion (as an interpreter), but some of its built-in commands do, like COPY.
Use a simple program to verify it.
#include <stdio.h>
int main(int argc, char *argv[]){
int i;
for (i = 1; i < argc; i ++)
printf("%s\n", argv[i]);
return 0;
}
Sample output on Unix:
$ ./a.out /*
/bin
/boot
/dev
...
/usr
/var
$ ./a.out /\*
/*
$
Sample output on Windows:
C:\Windows\system32>a.exe C:\*
C:\*
C:\Windows\system32>
If you want to let something else expand the wildcard for your program, use MinGW (MSYS) or Windows Subsystem for Linux (Win 10 1607 and later).
In Windows, wildcards are not supported, thus the error you see.
In UNIX though, what you used, would be correct:
Georgioss-MacBook-Pro:~ gsamaras$ nano main.c
Georgioss-MacBook-Pro:~ gsamaras$ nano fun.h
Georgioss-MacBook-Pro:~ gsamaras$ nano fun.c
Georgioss-MacBook-Pro:~ gsamaras$ gcc -c *.c
Georgioss-MacBook-Pro:~ gsamaras$

fltk complains about gcc on windows

I've been trying for almost a to build fltk on windows in so many different ways but I always end up with:
configure: error: C compiler cannot create executable
(msys fltk/configure)
or
Check for working C compiler: C:/MinGW/bin/gcc.exe -- broken
(CMake-gui)
I've tried it with fltk 1.3.0 and 1.3.3 and I've even used three different MinGW distros, GCC version: 5.1.0; 4.9.2 and 3.4.5. Yet it just won't compile. Here is some of the error log generated by the ./configure file:
$ ./configure --prefix=C:/libs/fltk-1.3.3
-----------
Core tests.
-----------
configure:1336: checking build system type configure:1354: result:
i686-pc-mingw32 configure:1362: checking host system type
configure:1376: result: i686-pc-mingw32 configure:1439: checking for
gcc configure:1455: found /c/mingw32/bin/gcc configure:1465: result:
gcc configure:1709: checking for C compiler version configure:1712:
gcc --version &5 gcc.exe (i686-win32-dwarf-rev2, Built by
MinGW-W64 project) 4.9.2 Copyright (C) 2014 Free Software Foundation,
Inc. This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
configure:1715: $? = 0 configure:1717: gcc -v &5 Using
built-in specs. COLLECT_GCC=c:\mingw32\bin\gcc.exe
COLLECT_LTO_WRAPPER=c:/mingw32/bin/../libexec/gcc/i686-w64-mingw32/4.9.2/lto-wrapper.exe
Target: i686-w64-mingw32 Configured with:
../../../src/gcc-4.9.2/configure --host=i686-w64-mingw32
--build=i686-w64-mingw32 --target=i686-w64-mingw32 --prefix=/mingw32 --with-sysroot=/c/mingw492/i686-492-win32-dwarf-rt_v4-rev2/mingw32 --with-gxx-include-dir=/mingw32/i686-w64-mingw32/include/c++ --enable-shared --enable-static --disable-multilib --enable-languages=ada,c,c++,fortran,objc,obj-c++,lto --enable-libstdcxx-time=yes --enable-threads=win32 --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-sjlj-exceptions --with-dwarf2 --disable-isl-version-check --disable-cloog-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=i686 --with-tune=generic --with-libiconv --with-system-zlib --with-gmp=/c/mingw492/prerequisites/i686-w64-mingw32-static --with-mpfr=/c/mingw492/prerequisites/i686-w64-mingw32-static --with-mpc=/c/mingw492/prerequisites/i686-w64-mingw32-static --with-isl=/c/mingw492/prerequisites/i686-w64-mingw32-static --with-cloog=/c/mingw492/prerequisites/i686-w64-mingw32-static --enable-cloog-backend=isl --with-pkgversion='i686-win32-dwarf-rev2, Built by MinGW-W64 project'
--with-bugurl=http://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -I/c/mingw492/i686-492-win32-dwarf-rt_v4-rev2/mingw32/opt/include -I/c/mingw492/prerequisites/i686-zlib-static/include -I/c/mingw492/prerequisites/i686-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe
-I/c/mingw492/i686-492-win32-dwarf-rt_v4-rev2/mingw32/opt/include -I/c/mingw492/prerequisites/i686-zlib-static/include -I/c/mingw492/prerequisites/i686-w64-mingw32-static/include' CPPFLAGS= LDFLAGS='-pipe
-L/c/mingw492/i686-492-win32-dwarf-rt_v4-rev2/mingw32/opt/lib -L/c/mingw492/prerequisites/i686-zlib-static/lib -L/c/mingw492/prerequisites/i686-w64-mingw32-static/lib -Wl,--large-address-aware' Thread model: win32 gcc version 4.9.2 (i686-win32-dwarf-rev2, Built by MinGW-W64 project) configure:1720:
$? = 0 configure:1722: gcc -V &5 gcc.exe: error:
unrecognized command line option '-V' gcc.exe: fatal error: no input
files compilation terminated. configure:1725: $? = 1 configure:1748:
checking for C compiler default output configure:1751: gcc
conftest.c >&5
c:/mingw32/bin/../lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x39):
undefined reference to `WinMain#16' collect2.exe: error: ld returned 1
exit status configure:1754: $? = 1 configure: failed program was: |
line 1728 "configure" | /* confdefs.h. */ | | #define PACKAGE_NAME "" | #define PACKAGE_TARNAME "" | #define PACKAGE_VERSION "" | #define
PACKAGE_STRING "" | #define PACKAGE_BUGREPORT "" | /* end confdefs.h.
*/ | | int main () | { | | ; | return 0; | } configure:1793: error: C compiler cannot create executables See `config.log' for more
details.
configure: exit 77
I previously compiled 2 other gui libraries with these same tools (nana and SFML), so there can't be anything wrong with the gcc compilers.
EDIT
Something (most probably cygwin) keeps breaking my mingw gcc program and every time its "broken" I get black vcc1.exe windows. Then I must replace the mingw folder to get it working again, this happens mostly when I open netbeans. I just don't know how to fix this, so I've just been avoiding cygwin and netbeans.
I was finally able to build it yesterday with this version of TDM-GCC (as per the gui installer):
MinGW-w64/TDM64 (32-bit and 64-bit) ==> TDM-GCC
I used he cmake gui instead of the .configure 1. open cmake gui. 2. input the source code folder (root of fltk folder, ie: "C:/libs/fltk-1.3.3"). 3. input the build folder, ie: "C:/libs/fltk-1.3.3/build/". 4. click configure 5. ** optionally you can change some options but i didn't 6. click Genertae 7. in cmd (I used git bash though): cd to the build directory and run make or mingw32-make
I completely uninstalled cygwin because of its interference with the tool chain builds, i.e mingw and tdm-gcc (Still not sure why this happens).
An easier way to setup a development environment is to use MSYS2.
Download and install
Run MSYS2 Shell
Install some dev tools anf FLTK: pacman -S make automake autoconf libtool mingw-w64-i686-gcc mingw-w64-i686-fltk
Run Mingw64 Shell and you'll be able to easily compile your source: g++ -o myproject myproject.cpp -lfltk

Why do I keep getting "Killed: 9" when I run this very basic SQLite3 based C file?

I have this super basic C file:
#include <sqlite3.h>
#include <stdio.h>
int main(void) {
printf("%s\n", sqlite3_libversion());
return 0;
}
And in the same directory I have sqlite3.h, sqlite3.c and sqlite3ext.h downloaded from the downloads page on sqlite.org.
I then run gcc -c main.c. Then chmod +x main.o. Then ./main.o. And every time I get:
Killed: 9
What am I doing wrong?
You cannot execute a relocatable object file directly like that. Try this:
gcc main.c -o main -lsqlite3
This works on Ubuntu with libsqlite3-dev package installed. Running main results in:
3.8.2
The -o flag specifies the name of the executable file. If you ommit -o main, you'll get a file called a.out with gcc on most platforms (maybe a.exe on windows+cygwin?). Either way, this file will already be executable, so you can skip the chmod +x.
The -lsqlite3 flag tells the compiler to link in the sqlite3 library too.
If you've built sqlite3 from scratch, you may also need -I and -L flags to tell the compiler where to look for libraries and headers.
In your command, the "-c" flag skips the linking stage and produces a relocatable object, where otherwise, gcc will produce an executable file.
You can use readelf -h main.o using output of your original command and readelf -h main using output of my suggested command, or alternatively just file main.o and file main to see differences in file types.

Resources