A
A
Aleksandr Sechko2018-05-11 23:45:21
C++ / C#
Aleksandr Sechko, 2018-05-11 23:45:21

Why can't the linker see the function from the static library?

I work in Kubuntu. One project needed to write its own static C library and add it to the qt project. gcc compiler.
The actual error report:

g++ -Wl,-rpath,/opt/Qt/5.11.0/gcc_64/lib -o M_un main.o mainwindow.o moc_mainwindow.o   -L/./lib/libmy2.a -L/opt/Qt/5.11.0/gcc_64/lib -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread 
mainwindow.o: In function `MainWindow::MainWindow(QWidget*)':
/home/aleksandr/build-M_un-Desktop_Qt_5_11_0_GCC_64bit-Debug/../M_un/mainwindow.cpp:22: undefined reference to `show_sign'
collect2: error: ld returned 1 exit status
Makefile:258: recipe for target 'M_un' failed
make: *** [M_un] Error 1

Well, the files:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include "string.h"

extern "C"
{
    #include <mylib.h>

}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QString s;
    ui->setupUi(this);
    unsigned int sign;
    float x;
    bool ok;
    x = 34.7;
    sign = show_sign(x);
    s = QString::number(sign);
    ui->sign->setText(s);
}

MainWindow::~MainWindow()
{
    delete ui;
}

M_un.pro
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = M_un
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS


SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui
INCLUDEPATH += ./lib
LIBS += -L/./lib/libmy2.a

project_lib.c (renamed to libmy2.a during build)
#include <stdlib.h>
 unsigned int show_sign(float x)
{
  float f = x;
  unsigned int* p = (unsigned int*) &f;
  unsigned int k = 2147483648;
  unsigned int u = (p[0] & k)/k;
  return u;
}

libmy2.h Here's the trouble. I've searched a bunch of forums and can't find a solution.
extern unsigned int show_sign(float x);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2018-05-12
@Hitrou

Drop -L from -L/./lib/libmy2.a.
-L/./lib/libmy2.asays "look for libraries in the directory /./lib/libmy2.a, but firstly it's not a directory, and secondly it doesn't say anywhere that you need to link with the libmy2.a library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question