这是我的第一个问题,请耐心等待。
我需要以头文件和.so文件的形式向我的qt项目添加一个外部库。如果仅在main.cpp中包含头文件(figure.h),则程序将按预期进行编译和运行。如果仅将头文件包含在scene.cpp中,则程序将再次编译。但是,当我将头文件添加到scene.h时,编译器会指出存在重复的符号,即使该头文件未包含在项目中的其他位置也是如此。有没有人可以帮忙?这让我难过了一段时间。
.pro文件:
QT += core gui widgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = moebinv-gui
TEMPLATE = app
INCLUDEPATH += /usr/local/include $$PWD/include
DEPENDPATH += /usr/local/include $$PWD/include
QMAKE_LFLAGS += -v
unix: LIBS += -L/usr/local/lib/ -lginac -lcycle -lfigure
SOURCES += \
src/main.cpp \
src/mainwindow.cpp \
src/scene.cpp
HEADERS += \
include/mainwindow.h \
include/scene.h
scene.h:
#ifndef SCENE_H
#define SCENE_H
#include <QGraphicsScene>
#include <figure.h>
class graphicsScene : public QGraphicsScene
{
Q_OBJECT
private:
MoebInv::figure *f;
};
#endif // SCENE_H
编译器错误:
duplicate symbol __ZN7MoebInv11default_asyE in:
main.o
mainwindow.o
duplicate symbol __ZN7MoebInv17evaluation_assistE in:
main.o
mainwindow.o
duplicate symbol __ZN7MoebInv8metric_pE in:
main.o
mainwindow.o
duplicate symbol __ZN7MoebInv13default_labelE in:
main.o
mainwindow.o
duplicate symbol __ZN7MoebInv8metric_hE in:
main.o
mainwindow.o
duplicate symbol __ZN7MoebInv8metric_eE in:
main.o
mainwindow.o
duplicate symbol __ZN7MoebInv11default_asyE in:
main.o
scene.o
duplicate symbol __ZN7MoebInv17evaluation_assistE in:
main.o
scene.o
duplicate symbol __ZN7MoebInv8metric_pE in:
main.o
scene.o
duplicate symbol __ZN7MoebInv13default_labelE in:
main.o
scene.o
duplicate symbol __ZN7MoebInv8metric_hE in:
main.o
scene.o
duplicate symbol __ZN7MoebInv8metric_eE in:
main.o
scene.o
duplicate symbol __ZN7MoebInv11default_asyE in:
main.o
moc_mainwindow.o
duplicate symbol __ZN7MoebInv17evaluation_assistE in:
main.o
moc_mainwindow.o
duplicate symbol __ZN7MoebInv8metric_pE in:
main.o
moc_mainwindow.o
duplicate symbol __ZN7MoebInv13default_labelE in:
main.o
moc_mainwindow.o
duplicate symbol __ZN7MoebInv8metric_hE in:
main.o
moc_mainwindow.o
duplicate symbol __ZN7MoebInv8metric_eE in:
main.o
moc_mainwindow.o
duplicate symbol __ZN7MoebInv11default_asyE in:
main.o
moc_scene.o
duplicate symbol __ZN7MoebInv17evaluation_assistE in:
main.o
moc_scene.o
duplicate symbol __ZN7MoebInv8metric_pE in:
main.o
moc_scene.o
duplicate symbol __ZN7MoebInv13default_labelE in:
main.o
moc_scene.o
duplicate symbol __ZN7MoebInv8metric_hE in:
main.o
moc_scene.o
duplicate symbol __ZN7MoebInv8metric_eE in:
main.o
moc_scene.o
ld: 24 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [moebinv-gui.app/Contents/MacOS/moebinv-gui] Error 1
12:08:20: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project moebinv-gui (kit: Desktop Qt 5.11.1 clang 64bit)
When executing step "Make"
答案 0 :(得分:1)
您不能在头文件中定义全局变量。例如,您有:
asy_style default_asy=asy_cycle_color;
您必须在标头中将变量声明为extern
:
extern asy_style default_asy;
然后在一个cpp文件中定义它:
asy_style default_asy=asy_cycle_color;
或者如果全局实际上是一个常量,则可以在标头中定义它:
const asy_style default_asy=asy_cycle_color;