Results 1 to 5 of 5

Thread: How to use methods from linked library written in Ansi C?

  1. #1
    Join Date
    Jun 2009
    Location
    Krakow, Poland
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to use methods from linked library written in Ansi C?

    Hello.
    I have library that has been written in Ansi C named "edr.a" and header file "edr.h". Library hass been compiled on 32-bit ubuntu and is linked into project but I get undefined references to all methods defined in header file.
    Please take a look on this simple example:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "edr.h"
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10.  
    11. SetDebugMode(); // this is method defined in edr.h
    12. }
    To copy to clipboard, switch view to plain text mode 
    edr.h file:
    Qt Code:
    1. long SetDebugMode(void);
    2. long ClearDebugMode(void);
    To copy to clipboard, switch view to plain text mode 
    This is what compilator is showing:
    Qt Code:
    1. g++ -c -m32 -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib64/qt4/mkspecs/linux-g++-32 -I../Lib -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I../LinkowanieBiblioteki -I../Lib -I. -I. -I. -o moc_mainwindow.o moc_mainwindow.cpp
    2. g++ -m32 -o LinkowanieBiblioteki main.o mainwindow.o moc_mainwindow.o -L -ledr -lQtGui -lQtCore -lpthread
    3. mainwindow.o: In function `MainWindow':
    4. /home/neon/workspace/qt4/LinkowanieBiblioteki-build-desktop/../LinkowanieBiblioteki/mainwindow.cpp:14: undefined reference to `SetDebugMode'
    To copy to clipboard, switch view to plain text mode 
    What I'm doing wrong? How to use linked external library written in Ansi C?

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to use methods from linked library written in Ansi C?

    You library isn't being linked; that's what an undefined reference means.

    You'll have to add it to your project file, with something like 'LIBS += /path/to/my/library.a'

  3. #3
    Join Date
    Jun 2009
    Location
    Krakow, Poland
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use methods from linked library written in Ansi C?

    In my .pro file there is "LIBS += -L -ledr". Even when I declare full path to library file "LIBS += /home/user/project/edr.a", there is the same issue.

    This is what compile command:
    Qt Code:
    1. g++ -m32 -o LinkowanieBiblioteki main.o mainwindow.o moc_mainwindow.o -L -ledr -lQtGui -lQtCore -lpthread
    To copy to clipboard, switch view to plain text mode 
    When I copied library edr.a into destination folder "/home/user/project-build-desktop" and run this command
    Qt Code:
    1. g++ -m32 -o LinkowanieBiblioteki main.o mainwindow.o moc_mainwindow.o edr.a -lQtGui -lQtCore -lpthread
    To copy to clipboard, switch view to plain text mode 
    from console than it works. So it's looks like there is problem with path to library.
    Last edited by porterneon; 6th March 2011 at 08:55.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to use methods from linked library written in Ansi C?

    So it's looks like there is problem with path to library.
    If you want dynamic linking, -L switch requires a path to directory where your library (.so on Linux or .dll on windows) is:
    Qt Code:
    1. LIBS += -L"/home/user/project/" -ledr
    To copy to clipboard, switch view to plain text mode 
    If you want static linking (.a or .lib), do as SixDegrees written (without the -ledr switch).

  5. #5
    Join Date
    Jun 2009
    Location
    Krakow, Poland
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use methods from linked library written in Ansi C?

    Hi all, thanx for reply.
    I did static linking in simple project with MainWindow widget only and it works. Than I tryied to do the same in my project (now there is 7 form widgets). I had clined solution, rebuild everything and again undefined references. Everything I have made in the same way. What is wrong?

    PS: I solved issue. extern "C" has fixed it.
    Qt Code:
    1. extern "C"{
    2. #include "edr.h"
    3. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by porterneon; 6th March 2011 at 12:32.

Similar Threads

  1. Replies: 0
    Last Post: 22nd February 2010, 16:36
  2. Adding Library Methods - Static Vs Dynamic
    By waynew in forum Qt Programming
    Replies: 1
    Last Post: 15th January 2010, 16:16
  3. Using ANSI string type
    By kahahn in forum Newbie
    Replies: 1
    Last Post: 20th June 2009, 23:52
  4. QHash non-ANSI key problem
    By roxton in forum Qt Programming
    Replies: 4
    Last Post: 27th May 2008, 15:54
  5. ISO/ANSI standard with Visual studio 2005
    By moowy in forum General Programming
    Replies: 7
    Last Post: 16th April 2007, 12:04

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.