Using dll library in release, problems.
-
Hi!
I have some problem, maybe someone know, what i do wrong.
Qt 4.7.4, Compiler: MinGW, OS: Windows 7
All the time I’ve been working in debug mode, but when I want to make release and try to compile in Release mode it crashes with
an error: “Segmentation fault”(In debug mode it works correct). I took a look at the application and found, that it happens when I try to use some functions from library, but I do not know, where I have made a mistake.I tested, for example, swe_get_planet_name and it works, but swe_julday doesn't(Segmentation fault).
Can somebody help me?
This is my class to abstract library.
@
#ifndef SWEDLL_H
#define SWEDLL_H
#include <QLibrary>class swedll
{
private:
QLibrary swedll32;
int isloaded;
int count;typedef void (* Swe_Set_Ephe_Path) (QString);
typedef double(* Swe_Julday) (int, int, int, double, int);
typedef char* (* Swe_Get_Planet_Name) (int, char );
typedef int ( Swe_Calc_Ut) (double, int, int, double*, char*);
typedef double(* Swe_Sidtime) (double);
typedef int (* Swe_Houses_Armc) (double, double, double, int, double*, double*);
typedef int (* Swe_Houses) (double, double, double, int, double*, double*);public:
swedll(); ~swedll();
void swe_set_ephe_path(QString);
QString swe_get_planet_name(int);
double swe_julday(int, int, int, double);
double* swe_calc_ut(double, int);
double* swe_calc(double, int, int);
double swe_sidtime(double);
double* swe_houses_armc(double, double, double, int);
};#endif // SWEDLL32_H
#include “swedll.h”
swedll::swedll()
{
swedll32.setFileName(“swedll32”);
}swedll::~swedll()
{}
void swedll::swe_set_ephe_path(QString Path)
{
swedll32.load();
Swe_Set_Ephe_Path set_ephe_path = (Swe_Set_Ephe_Path)swedll32.resolve(”_swe_set_ephe_path@4”);
if(set_ephe_path)set_ephe_path(Path);
}QString swedll::swe_get_planet_name(int Number)
{
swedll32.load();
char x[256];
Swe_Get_Planet_Name get_planet_name = (Swe_Get_Planet_Name)swedll32.resolve(”_swe_get_planet_name@8”);
QString result = “”;
if(get_planet_name) result+= get_planet_name(Number, x); return result;
}double swedll::swe_julday(int Day, int Month, int Year, double Hour)
{
swedll32.load();
Swe_Julday julday = (Swe_Julday)swedll32.resolve(”_swe_julday@24”);
if(julday)return julday(Day, Month, Year, Hour, 1); else return 0;
}double* swedll::swe_calc_ut(double Julday, int PlanetNumber)
{
return swe_calc(Julday, PlanetNumber, 256);
}double* swedll::swe_calc(double Julday, int PlanetNumber, int Ephe)
{
swedll32.load();
Swe_Calc_Ut calc_ut = (Swe_Calc_Ut)swedll32.resolve(”_swe_calc_ut@24”);
char err[256];
double *result = new double[6];
if(calc_ut)calc_ut(Julday, PlanetNumber, Ephe, result, err); return result;
}double swedll::swe_sidtime(double Time)
{
swedll32.load();
Swe_Sidtime sidtime = (Swe_Sidtime)swedll32.resolve(”_swe_sidtime@8”);
if(sidtime)return sidtime(Time); else return 0;
}double* swedll::swe_houses_armc(double ARMC, double geolat, double eps, int hsys)
{
swedll32.load();
double *cusps = new double[13];
double *ascmc = new double[10];
Swe_Houses_Armc houses_armc = (Swe_Houses_Armc)swedll32.resolve(”_swe_houses_armc@36”);
if(houses_armc)houses_armc(ARMC, geolat, eps, hsys, cusps, ascmc); return cusps;
}
@
These functions from this .dll work with Delphi(before started Qt i had been developing it in Delphi), so that it can not be a library problem.This is a code from source(swedll32.h) for this functions:
@
DllImport int32 FAR PASCAL swe_calc_ut( double tjd, int ipl, int32 iflag, double *xx, char *serr);DllImport double FAR PASCAL swe_sidtime(double tjd_ut);
DllImport int FAR PASCAL swe_houses_armc( double armc, double geolat, double eps, int hsys, double *hcusps, double *ascmc);
DllImport double FAR PASCAL swe_julday( int year, int mon, int mday, double hour, int gregflag);
DllImport char *FAR PASCAL swe_get_planet_name(int ipl, char *spname);
DllImport void FAR PASCAL swe_set_ephe_path(char *path);@
-
If the dll isn't compiled with the same compiler as your app, I believe there can be problems. See if this "thread":http://developer.qt.nokia.com/forums/viewthread/2046/P15 helps any.
-
Ok, I have tried to compile it on Linux(Ubuntu) now with the same compiler(GCC) a library from source(Unix Source http://www.astro.com/ftp/sweph/swe_unix_src_1.77.00.tar.gz). And change a name of Library from sweph32 to libswe (.so) in code. Now Qt does't load a library in debug or release...Qt is hard...
-
So, you switched OS? or did you only compile the library new but are working on windows?
Thios is not a Qt problem at all. Loading dlls and resolving names is an OS and compiler issue.Are you still working on windows or on linux?
Which compiler tool chain do you use for what?
Which errors do you get?
Where are the libraries and executables placed?
How does your environment look like (PATH, etc.)
-
I have swithced OS to try to use the same Compiler(gcc, toolchain GCC, Linux Ubuntu on my OS for work), but it doesn't work. It is, what I have read here http://developer.qt.nokia.com/forums/viewthread/2046/P15 (to use the same compiler). I tried it on linux, because it is more easily to compile on linux library and project with the same compiler.
At first I have tried it in Windows(because my programm was written for Windows Users and it will be the best solution to compile it in Windows), a have tried to solve this problem in Windows, but i cann't it solve myself.
This library works correctly in Qt in debug mode and in delphi projekt in Windows, in Release mode works only 2-3 functions correctly, others fail.
My possibilities:
-
try to use MSVC2008 compiler
-
try to compile library with MinGW
??? -
I work on windows(I want to find solution for windows)
-
MinGW as a GCC for Windows targets
-
I get runtime error(only if try to debug in Release) SIGSEGV Segmentation fault, otherwise it crash's without error information. In debug mode it works.
-
Library(sweph.dll) is placed in folder with project and in build-desktop folder
-
System Path looks like "C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\Broadcom\Broadcom 802.11 Network Adapter\Driver;C:\Program Files (x86)\Toshiba\Bluetooth Toshiba Stack\sys;C:\Program Files (x86)\Toshiba\Bluetooth Toshiba Stack\sys\x64"
I compile my project from Qt Creator.
-
-
It is not small, but very easy and understandable.
I hope it is not too big.
I had created GUI Application.
main.cpp
(Creator generate)
@#include <QtGui/QApplication>
#include "window.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window w;
w.show();return a.exec();
}@
With a class Window
window.h
@
#ifndef WINDOW_H
#define WINDOW_H#include <QMainWindow>
#include <lib.h> // Include my classnamespace Ui {
class Window;
}class Window : public QMainWindow
{
Q_OBJECTpublic:
explicit Window(QWidget *parent = 0);
~Window();private:
Ui::Window *ui;
};#endif // WINDOW_H@
window.cpp
@#include "window.h"
#include "ui_window.h"Window::Window(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Window)
{
ui->setupUi(this);
QDateTime DT;
DT = ui->dateTimeEdit->dateTime(); // I take time from dateTimeEdit
// Make operation to have day, month, year and hour
int day = DT.date().day();
int month = DT.date().month();
int year = DT.date().year();
int hour = DT.time().hour();
double min = (double)DT.time().minute() / 60.0;
double sec = (double)DT.time().second() / 3600;
double dhour = hour + min + sec;
// get instance of my class Library
lib Lib;
// Try to use it with default date 01.01.2000 00:00:00
double tjd = Lib.swe_julday(year, month, day, dhour);
// Try secondtime....It crashs now
tjd = Lib.swe_julday(year, month, day, dhour);ui->label->setText(QString::number(tjd, 'g', 8));
}Window::~Window()
{
delete ui;
}@This is small library abstraction:
@#ifndef LIB_H
#define LIB_H
#include <QLibrary>
#include <QDateTime>class lib
{
private:
QLibrary swedll;typedef double(* Swe_Julday) (int, int, int, double, int);
public:
lib();double swe_julday(int, int, int, double);
};
#endif // LIB_H
@@#include "lib.h"
lib::lib()
{
swedll.setFileName("swedll32");
}double lib::swe_julday(int Day, int Month, int Year, double Hour)
{
swedll.load();
Swe_Julday julday = (Swe_Julday)swedll.resolve("_swe_julday@24");
if(julday)return julday(Day, Month, Year, Hour, 1); else return 0;
}
@An xml, but I have only DateTimeEdit and Label, nothing else
@<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Window</class>
<widget class="QMainWindow" name="Window">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Window</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QDateTimeEdit" name="dateTimeEdit">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>194</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>50</x>
<y>70</y>
<width>171</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
@This is a stuff, that I used ftp://ftp.astro.com/pub/swisseph/sweph.zip, this is a Source, compiled dll and Example in C, Delphi and Documentation.
-
Hi :-) Has anybody tried my example??
I have tried this library to compile in Linux Ubuntu 11.04 from Unix Source.
And I have used JNA to load a library and use in Java in Ubuntu. Library works correctly in java.And now I have tried it with Qt the same shared library(I copy libswe.so in src folder in a folder with execute program in usr/lib/ folder - it must be found from Qt, I have tried swedll32.setFileName("libswe"); and so(as in JNA) swedll32.setFileName("swe"); ), with the same Qt C++ code. It doesn't work. But hier it can't load( isloaded = swedll32.isLoaded(); after this isloaded == 0 ) a library.
What do I wrong?
-
May be Qt is not hard, but C++(or C++ with Qt) is really hard for me. I have tried to find a bug for a month and I cann't. I see no other way except of redeveloping the program with other toolkit.