【问题标题】:Qt static lib submodule buildQt 静态库子模块构建
【发布时间】:2019-11-12 18:45:54
【问题描述】:

我想自动构建第三方库并将它们包含在我的 Qt 项目中。

我的 Qt 项目的 .pro 文件如下所示:

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

QMAKE_EXTRA_TARGETS += stalib
PRE_TARGETDEPS += stalib
stalib.commands = make ../thirdparty/stalib/Makefile

LIBS += -L$${PWD}/../thirdparty/stalib/lib -lStalib

INCLUDEPATH += $${PWD}/../thirdparty/stalib/src

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += main.cpp

我的第三方依赖子模块的 Makefile 如下所示:

#-----------------------------------------------#
#     Makefile for hello world                  #
#-----------------------------------------------#
# add preprocessor define
DEF_PARAMS = -DGREET=\"world\"

# Just to make sure
ifndef CROSS_COMPILE
$(error "cross-compilation environment not activated properly")
endif

# add debug symbols, DO NOT overwrite contents of FLAGS, append!
CFLAGS += -g $(DEF_PARAMS)

#Compilation only ignore warnings (ignore/-w, show all/-Wall).
CFLAGS   += -c -w
SOURCEDIR=./src
LIBDIR=./lib
#-----------------------------------------------#
#     Project Specific Settings                 #
#-----------------------------------------------#
# include directories relative to $SDKTARGETSYSROOT/usr/include (system HEADERS) or this Makefile (project headers).
INC_PARAMS = $(SOURCEDIR)
# project headers
HEADERS = $(SOURCEDIR)/math.h
# project sources
SOURCES = $(SOURCEDIR)/math.c

# Object files.
OBJECTS=$(SOURCES:%.c=%.c.o)

# Link libraries
# Libraries search directories relative to $SDKTARGETSYSROOT/usr/libs
# Library name without lib and .so e.g. libm.so -> -lm.
LINK_LIBS=

#Target name
TARGET_STATIC = $(LIBDIR)/libStalib.a

#-----------------------------------------------#
#     Print Make Parameters                     #
#-----------------------------------------------#
print-%:
    @echo "SOURCES=$(SOURCES)"
    @echo "HEADERS=$(HEADERS)"
    @echo "DEF_PARAMS=$(DEF_PARAMS)"
    @echo "CFLAGS=$(CFLAGS)"
    @echo "LDFLAGS=$(LDLAGS)"
    @echo $* = $($*)

#-----------------------------------------------#
#     Makefile Make Executable                  #
#-----------------------------------------------#
.SUFFIXES: .c

#Build rules begin.
all: $(TARGET_STATIC)

#Build rule for static library target.
$(TARGET_STATIC): $(OBJECTS)
    $(AR) rc $@ $(OBJECTS)

#Build rule for dynamic library target.
$(TARGET_SHARED): $(OBJECTS)
    $(LD) $(LDFLAGS) $(OBJECTS) $(LINK_LIBS) -o $@

#Build rule for executeable target
$(TARGET): $(OBJECTS)
    $(CC) $(LDFLAGS) $^ $(LINK_LIBS) -o $@

#Compilation rule for c files.
%.c.o: %.c $(HEADERS)
    $(CC) $(CFLAGS) $(INC_PARAMS) $< -o $@

#Clean-up object files and target.
clean:
    rm  -f $(OBJECTS) $(TARGET) $(TARGET_STATIC) $(TARGET_SHARED)

但是,我在构建时遇到链接器错误。找不到math.h文件中定义的函数:

#ifndef MATHH
#define MATHH

int addNums(int a, int b);

#endif

但奇怪的是,QtCreator 竟然能够跟随对头文件的引用。

对于所有想要直接检查来源或摆弄它们的人: https://github.com/faxe1008/myapp https://github.com/faxe1008/stalib

感谢任何有关如何改进的帮助或建议。

【问题讨论】:

  • libStalib.a 正在构建吗?
  • 不是通过 pro 文件,而是手动调用 make 作品
  • 如果您手动构建它是否链接正常?
  • 不,这两种方式都没有

标签: c qt makefile


【解决方案1】:

如果你想自动构建库,那么你需要在你的 .pro 中修改这一行:

stalib.commands = make -C../thirdparty/stalib CROSS_COMPILE=1

但这不是你的问题。你没有显示你的 .cpp 代码,但我猜你忘了像这样包围你的#include

extern "C" {
    #include "math.h"
}

没有它,你不能在 C++ 源代码中包含非系统 C 头文件。见:https://isocpp.org/wiki/faq/mixing-c-and-cpp

【讨论】:

  • 现在至少可以链接了,非常感谢。对于 pro 文件部分:我在构建 qmake 文件之前采购了一个环境设置脚本。显然那些没有向下传递到makefile。我该怎么做?
  • 小心。如果在子进程中运行设置脚本,则脚本定义的环境变量在父进程或其兄弟进程中将不可用。也许您可以在调用 make 之前在相同的 stalib.commands 中运行设置脚本?
猜你喜欢
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
相关资源
最近更新 更多