Linker error with CMake - sqlite

I'm using JetBrains CLion for pure C (C ANSI) development, I know it's target is C++, but my company works only with C and
CLion uses only CMake as build system.
My system is a Debian Jessie system and sqlite3 and libsqlite3-dev are installed.
I'm trying to build a simple sqlite3 project like this:
#include <sqlite3.h>
#include <stdio.h>
int main() {
sqlite3 *sqlConnection;
int ret;
ret = sqlite3_open("database/path.db, &sqlConnection);
if (ret) {
printf("Ups ... can't open %d", ret);
}
do_some_queries(sqlConnection);
return 0;
}
The automatic generated CMakeLists.txt is the follwing.
cmake_minimum_required(VERSION 3.3)
project(Project)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp )
add_executable(Project ${SOURCE_FILES})
When build, either through Clion, either through command line, I get linker errors:
...
undefined reference to `sqlite3_prepare_v2'
undefined reference to `sqlite3_column_int'
undefined reference to `sqlite3_open'
...
I know I must point out to CMake where sqlite3 is, but I can't find a way of doing this.
"find_package" and "find_library" may do it, but I can't find how.
I've also found this Cmake file, but could not used successfully.
So, how do I integrate sqlite3 with Cmake ?

You need to add the path to sqlite header file to your include path. Then link the sqlite library using target_link_libraries:
https://cmake.org/cmake/help/v3.3/command/target_link_libraries.html

I've founded a workaround, but it's not the correct way of doing.
Since on Debian the "gcc main.c -lsqlite3" works (with libsqlite3-dev installed), passing the -lsqlite3 flag to the linker do the trick.
so I've changed
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
to
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lsqlite3")
And it worked.

Related

Adding a custom sqlite function to a Qt application in linux

I am trying to add a custom sqlite3 regexp function into my Qt application (as recommended by this answer). it works fine in windows but in fedora i get this error:
:-1: error: sqlite3.o: undefined reference to symbol 'dlclose##GLIBC_2.2.5'
after adding LIBS += -ldl to .pro, compile is ok but when run command query.exec(); return value is false without any description and qtregexp() function don't run.
Edit:
after comment
LIBS += -ldl
SOURCES += sqlite\sqlite3.c
HEADERS += sqlite\sqlite3.h
and add
LIBS += -lsqlite3
to .pro i get segmentation fault with sqlite3_create_function .
if i open a database with native sqlite3 API (sqlite3_open) Instead handle of QSqlDatabase , sqlite3_create_function works very well

undefined reference to library function in Qtcreator

I have to use a library under Linux. It's a .so compiled with gcc.
I added on my .pro :
INCLUDEPATH += mypath/include
LIBS += -L/mypath/lib/ -lmyLib
but, i get the following error :
undefined reference to `init_glove(char*, char*)'
I don't understand why i get this message. My library is linked and the header file is founded. I read that it could be a problem between the compiler used for my library, and the one used by Qt, but both seem to be gcc, so...
Any ideas ?
If the problem was the g++/gcc issue, then in your QT project simply do this
extern "C"
{
#include "mylib.h"
}
Actually, Qt was compiling with g++, so I had to change de compiler of my library : switching it from gcc to g++.

Qt Creator release mode undefined references to std::out_of_range

I'm writing a small C++ program (with a GUI) with Qt Creator and compiling with MinGW. Everything works fine when I compile the project in debug mode but as soon as I move to release mode I get compiler errors:
undefined reference to 'std::out_of_range::~out_of_range()' thread.cpp
When I click on the error I also get:
File not found: thread.cpp
I have looked through my Boost installation and found thread.cpp and it should be on the include path for my project.
Any ideas?
EDIT: Here is my .pro file:
#-------------------------------------------------
#
# Project created by QtCreator 2012-08-10T12:09:39
#
#-------------------------------------------------
QT += core gui
TARGET = GeneDropWin
TEMPLATE = app
SOURCES += main.cpp \
genedrop.cpp \
mainbody.cpp \
biofunctions.cpp \
fileio.cpp \
settings.cpp
HEADERS += genedrop.h \
geneclasses.h \
paramclass.h \
mainbody.h \
biofunctions.h \
fileio.h \
geneclasses.h \
settings.h
FORMS += genedrop.ui \
settings.ui
#Stuff I've added
INCLUDEPATH += "C:\\Program Files\\boost_1_50_0"
LIBS += -L"C:\\Program Files\\boost_1_50_0\\stage\\lib" -lboost_thread-mgw46-mt-1_50 -lboost_system-mgw46-mt-1_50 -lboost_date_time-mgw46-mt-1_50 -lboost_chrono-mgw46-mt-1_50
CONFIG += static \
release
RESOURCES += \
NIABLogo.qrc
Ok, the problem is fixed but perhaps not completely understood. I fixed it by switching compiler to MSVC and changing the syntax for the linker options (e.g. -lboost_thread-mgw46-mt-1_50 -> -llibboost_thread-vc100-mt-1_50), compiles without an issue now. I will put forward my reasoning as to what I think may have been the problem but would appreciate a better answer if wrong:
Although I thought I had built the Boost libraries with MinGW when looking through the installation I found a number of folders mentioning msvc instead (e.g. ...boost\bin.v2\libs\date_time\build_msvc-10.0) which suggested to me that I had built it with MSVC.
The linker error claimed not to be able to find files associated with thread.cpp.
Looking at the thread folder of the build directory all the .obj and .lib files had msvc-10.0 folders in their path.
Thus I think that the MinGW compiler was looking for boost objects within a non-existent MinGW folder and so was failing. The fact that it worked under debug mode I guess relates to a less-constrained search for files.

Help getting started with Qt

I am a newby in QT and cannot even get a starting example to work. The problem is that even QtCore is not found. Think something wrong with my path or not the right version is used?
#include <QtCore>
#include <iostream>
#include <QtXml/QXmlSimpleReader>
int main(int argc, char *argv[])
{
QDir xmldir("/xxx/xxx");
QXmlSimpleReader xmlReader;
}
Example of error is:
/Users/frank/xxx-build-desktop/../xxx/main.cpp:1: error: QtCore: No such file or directory.
Checked the path and the path to qmake is /usr/bin/qmake
If I run qmake -v this is printed:
QMake version 2.01a
Using Qt version 4.7.0 in /Library/Frameworks
I am using Mac 10.6.6
Any reply will be appreciated.
Here is the project file:
QT += core gui
QT += xml
QT += webkit
QT += xmlpatterns
TARGET = xxx
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
Results from make:
iMac:~/xxx/ qmake -spec macx-g++
iMac:~/xxx/ ls -ltr
total 56
-rw-rw---- 1 frank staff 349 Feb 5 16:06 xxx.pro
-rw-rw---- 1 frank staff 3875 Feb 6 12:56 main.cpp
-rw-rw---- 1 frank staff 7985 Feb 6 12:56 xxx.pro.user
-rw-rw---- 1 frank staff 8974 Feb 6 12:56 Makefile
iMac:~/xxx/ make
g++ -c -pipe -g -gdwarf-2 -Wall -W -DQT_WEBKIT_LIB -DQT_XMLPATTERNS_LIB -DQT_XML_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Qt4.7/mkspecs/macx-g++ -I. -I. -I/usr/include/QtCore -I. -I/usr/include/QtGui -I. -I/usr/include/QtXml -I. -I/usr/include/QtXmlPatterns -I. -I/usr/include/QtWebKit -I/usr/include -I. -F/Library/Frameworks -o main.o main.cpp
main.cpp:1:18: error: QtCore: No such file or directory
main.cpp: In function ‘int main(int, char**)’:
main.cpp:27: error: ‘QDir’ was not declared in this scope
main.cpp:27: error: expected `;' before ‘xmldir’
main.cpp:28: error: ‘xmldir’ was not declared in this scope
main.cpp:28: error: ‘QDir’ is not a class or namespace
main.cpp:28: error: ‘QDir’ is not a class or namespace
main.cpp:28: error: ‘QDir’ is not a class or namespace
main.cpp: At global scope:
main.cpp:25: warning: unused parameter ‘argc’
main.cpp:25: warning: unused parameter ‘argv’
main.cpp:63: warning: unused parameter ‘namespaceURI’
main.cpp:63: warning: unused parameter ‘localName’
main.cpp:63: warning: unused parameter ‘qName’
main.cpp:67: warning: unused parameter ‘namespaceURI’
main.cpp:67: warning: unused parameter ‘localName’
make: *** [main.o] Error 1
iMac:~/xxx/
For most C++ Qt applications you write, it's not immediately obvious how to compile the application by hand. This is complicated by a number of factors:
The header files may be placed in different directories depending on their version
moc, the meta-object compiler, needs to be run
The correct version of the DLLs need to be included
A number of preprocessor defines need to be in place
Given all of the above, it's easiest to use a build system like qmake, which is native to Qt, cmake, or some other build system that is Qt aware.
For people new to Qt, I recommend qmake.
Here's the basic command line usage. Qmake provides integration with both Visual Studio and XCode, but I won't address that here:
Create a directory for your project
Write whatever files you believe you need
Build a project file for qmake by running qmake on your project as follows: qmake -project "CONFIG+=xml". Since you're on mac, you might also want a -macx option, but I've never used it myself. At least in the past, qmake was not smart enough to pick up the XML dependency, so the "CONFIG+=xml" option adds it explicitly.
Create the makefiles from the project file by running qmake again: qmake
Now that you have makefiles, build the application using make, nmake, gmake, or the corresponding version of make for your system.
If everything has been picked up correctly by qmake, you should now be able to build your application by using make.
When you add new files to your project, they will need to be added to your project file (*.pro) and then qmake rerun without any options.
#includes are for header files. QtCore isn't a header, it's a directory that contains headers.
Since you're using QDir, change the first line to this:
#include <QtCore/QDir>
It is rather strange that your /usr/include/* directores don't exist. It seems that there is something wrong with the Qt installation. I don't understand Mac specifics, but from what I've been able to find out, it looks like the -F/Library/Frameworks parameter that make passes to G++ should allow you to use #include <QtCore/QtCore> instead of just #include <QtCore>. In a correct Qt installation on other platforms (Windows, Linux), both forms are equally acceptable though. But judging by the fact that G++ doesn't complain about the #include <QtXml/QXmlSimpleReader> line, the form #include <QtModule/ModuleHeader> works fine in your setup, so it could be used as a workaround, provided that there are no other problems.
Note that I wouldn't recommend to include QtCore in any form, though. It includes a lot of stuff most of which is probably useless to you and it will only increase compilation time and the probability of name conflicts. Include only what you intend to use instead, like #include <QDir> or #include <QtCore/QDir> in your case.
If you are new to Qt, and there's not special build requirement (PPC, 10.4, etc), I would strongly recommend that you download prepackaged Qt SDK of your platform. And use the included IDE, Qt Creator, to start. That will get your feet wet quickly and less painfully.
The variable settings you provided, which seems to be in your makefile, should actually be in a QMake file xxx.pro. You then run qmake, which generates the Makefile from xxx.pro, and then finally can run make. QMake uses variables such as QT and SOURCES, along with its knowledge of Qt and your installation, to produce a correct makefile.
If you do not find QtCore in /usr/include/, then this is an installation issue. In 4.7.0 I heard people having the same problem, so I guess there is a packaging problem for macs in 4.7.0.
Try to get 4.7.1 or build your own out of the sources.

How to use Dcmtk in Qt?

I am working on a project where I have to read a dicom image.
I am unable to install dcmtk. I am using win7 64-bit and vs2010.
Please explain the procedure to include dcmtk in my program.
To use the Windows + DCMTK + QT, you need to execute these follow steps:
Compile the DCMTK (Step 1.A)
Create your sample application (Step 2)
Create your QT project file referring the compiled/installed DCMTK Libs (Step 3.B)
Compile your application in your IDE (Step 4.B)
If you are reading this and don't want to use the Qt, I am answering a version without Qt as well.
Windows+VisualStudio+DCMTK: Steps 1.A, 2, 3.A and 4.A
Linux+GCC+DCMTK: Steps 1.B, 2, N/A, 4.C
1) Compile the DCMTK
First of all, to use the DCMTK library in your application, you should compile the DCMTK source code to generate the libraries:
While I am writing this, the last available version is 3.6.0. So, we should download it:
ftp://dicom.offis.de/pub/dicom/offis/software/dcmtk/dcmtk360/dcmtk-3.6.0.zip
After the download is finished, you need to unzip the folder. The DCMTK source doesn't have a project file, but it is not a problem, there is a CMakelist file responsible for generating a project file in your desirable Operational System/Compiler. If you are not familiar with CMake tool, you can read more here (https://cmake.org/)
1.A) Compiling on Windows/Visual Studio 2012
1.A.1) You need to have an installed compiler, in my case, it was the Visual Studio 2012
1.A.2) Run the CMake tool to generate a project file to Visual Studio 2012. You need be able to fill the source DCMTK directory.
1.A.3) Now, execute the VisualStudio 2012, open the file sln created in the previous step (2) and compile the target ALL_BUILD
1.A.4) Re-Execute VisualStudio in Admin mode (because of permission C:\Program Files) to compile the target INSTALL (it will copy and install the DCMTK to default path: C:/Program Files/DCMTK/, we can reference it such PATH_WHERE_DCMTK_WAS_INSTALLED)
(1.B) Compiling on GNU/Linux GCC
I have tested at Ubuntu/CentOS. The first, you should go to DCMTK Source and run these following three commands:
$ ./configure --prefix=path_to_dcmtk
$ make all
$ sudo make install
example: path_to_dcmtk = /home/user/dcmtk
2) Creating your sample application
Create a file called your_sample/testapp.cxx with the following content. This a demonstration found in DCMTK Forum to open a DICOM file and print the patient's name.
#include "dcmtk/dcmdata/dctk.h"
#include <iostream>
using namespace std;
int main()
{
DcmFileFormat fileformat;
OFCondition status = fileformat.loadFile("test.dcm");
if (status.good())
{
OFString patientsName;
if (fileformat.getDataset()->findAndGetOFString(DCM_PatientName, patientsName).good())
{
cout << "Patient's Name: " << patientsName << endl;
}else{
cerr << "Error: cannot access Patient's Name!" << endl;
}
}else{
cerr << "Error: cannot read DICOM file (" << status.text() << ")" << endl;
}
return 0;
}
3) Creating the Application Project using the VisuaStudio as Compiler
The created file at the previous step needs to be placed in a project. You can choose between the option Windows VisualStudio (3.1) and Windows Qt (3.2). If you are using Linux, you can skip this step.
3.A) Windows with Visual Studio 2012 IDE
To create a Visual Studio project, you can use the Wizard and set the necessary settings such: Linker, Libraries, etc. However, to make easier on this answer, I will use the CMakeList.txt to create a Project for Visual Studio 2012. So, please, create a file called your_sample/CmakeList.txt with the following content:
PROJECT(testapp)
SET(DCMTK_DIR ABSOLUTE_PATH_WHERE_DCMTK_WAS_INSTALLED)
#an example: SET(DCMTK_DIR "C:\\Users\\test\\test_dcmtk\\DCMTK")
# settings for Microsoft Visual C++ 6
SET(CMAKE_C_FLAGS "/nologo /W3 /GX /Gy /YX")
SET(CMAKE_C_FLAGS_DEBUG "/MTd /Z7 /Od")
SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
SET(CMAKE_CXX_FLAGS "/nologo /W3 /GX /Gy /YX")
SET(CMAKE_CXX_FLAGS_DEBUG "/MTd /Z7 /Od")
SET(CMAKE_CXX_FLAGS_RELEASE "/MT /O2")
ADD_DEFINITIONS(-D_REENTRANT)
INCLUDE_DIRECTORIES(${DCMTK_DIR}/include)
LINK_DIRECTORIES(${DCMTK_DIR}/lib)
ADD_EXECUTABLE(testapp testapp)
TARGET_LINK_LIBRARIES(testapp netapi32 wsock32 ofstd dcmdata)
3.B) Windows QtCreator IDE using the VisuaStudio as Compiler
To create a project file for QtCreator IDE. You need to create a file called your_sample/my_project.pro with the following content:
SOURCES += testapp.cxx
CONFIG += debug console
DEFINES += _REENTRANT
QMAKE_CFLAGS_RELEASE -= -MD
QMAKE_CFLAGS_RELEASE = -MT
QMAKE_CFLAGS_DEBUG -= -MDd
QMAKE_CFLAGS_DEBUG = -MTd
QMAKE_CXXFLAGS_RELEASE -= -MD
QMAKE_CXXFLAGS_RELEASE += -MT
QMAKE_CXXFLAGS_DEBUG -= -MDd
QMAKE_CXXFLAGS_DEBUG += -MTd
INCLUDEPATH += (RELATIVE_PATH_WHERE_DCMTK_WAS_INSTALLED)/include
#an example: INCLUDEPATH += ../../../test_dcmtk/DCMTK/include
LIBS += -L"(RELATIVE_PATH_WHERE_YOU_INSTALLED_DCMTK)/lib" \
-ladvapi32 \
-ldcmdata \
-loflog \
-lofstd \
-lws2_32 \
-lnetapi32 \
-lwsock32
#an example: LIBS += -L"../../../test_dcmtk/DCMTK/lib" \
4.A) Windows with Visual Studio 2012 IDE
Open the project file at VisualStudio and click on Build.
4.B) Windows with QtCreator IDE using the VisuaStudio as Compiler
Open the project file at QT and click on Build.
4.C) GNU/Linux - Command Line with GCC
g++ testapp.cxx -DHAVE_CONFIG_H -I/path_to_dcmtk/include -L/path_to_dcmtk/lib -pthread -ldcmdata -lz -loflog -lofstd -o main
Please, note that whether you have compiled the libraries in DEBUG mode, your application should be compiled in DEBUG mode as well.
References
How Install
http://support.dcmtk.org/docs/file_install.html
DCMTK Docs
http://support.dcmtk.org/docs/
FAQ #40: How do I use the DCMTK libraries in my own application? [MSVC]
http://forum.dcmtk.org/viewtopic.php?f=4&t=652
CMake Configuration
http://support.dcmtk.org/wiki/dcmtk/howto/cmakeconfiguration360
Try to follow the instructions for the Dcmtk installation. If you don't know how to include the library in your project, study the qmake manual.
The Common Toolkit guys have addressed some of these issues. You could also use the Insight Toolkit
http://www.commontk.org/index.php/Main_Page
http://itk.org
In fact there is a bit of documentation on the ITK wiki, but ITK uses gdcm instead of dcmtk.
http://www.itk.org/Wiki/ITK_FAQ#How_to_read_a_volume_from_a_DICOM_series
We have a project that is an implementation of DCMTk in Qt -- we call it QtDICOM, and it forms the foundation of many of our products (http://fluxinc.ca/medical)/. Worth a look (https://bitbucket.org/fluxinc/qt-dicom). We haven't documented the configuration particularly well, but you can probably figure it out if you have a look.
Note: It does depend on DCMTk being built and included in the various paths.

Resources