Search code examples
c++cqtgnu-makeqmake

Symbol(s) not found while writing a Qt program using existing C program


I wrote an ftp client in C (using GNU make) and now want to use Qt to write a GUI for it.

While building, error occures:

error: symbol(s) not found for architecture x86_64
linker command failed with exit code 1 (use -v to see invocation)
Undefined symbols for architecture x86_64:
  "showHelpMsg()", referenced from:
  _main in main.o

My main.cpp looks like this:

include "client.h" 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ftpClient w;
    w.show();
    showHelpMsg();
    return a.exec();
}

client.h:

#include"common.h"
#include"handler.h"
#include"util.h"    //These headers works fine while using gnu make    
void showHelpMsg();

client.c:

include "client.h" 
void showHelpMsg()
{
    //....
}

And my project.pro:(auto generated by Qt)

SOURCES += main.cpp\
    ftpclient.cpp \
client.c \
common.c \
handler.c \
util.c

HEADERS  += ftpclient.h \
client.h \
common.h \
handler.h \
util.h

For reference, this is my previous makefile:

objects = client.o handler.o util.o common.o

server : $(objects)
cc -Wall -o client $(objects)

.PHONY : clean
clean :
    rm client $(objects)

Solution

  • Try changing client.h to this:

    #ifdef __cplusplus
    extern "C" {
    #endif
    #include"common.h"
    #include"handler.h"
    #include"util.h"
    void showHelpMsg();
    #ifdef __cplusplus
    }
    #endif
    

    The problem might be that the C++ compiler is expected showHelpMsg to be a C++ function with a mangled name, while the C compiler is only producing a normal C function with a normal name.