首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Qt静态库子模块构建

Qt静态库子模块构建
EN

Stack Overflow用户
提问于 2019-11-13 02:45:54
回答 1查看 139关注 0票数 1

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

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

代码语言:javascript
复制
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

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

代码语言:javascript
复制
#-----------------------------------------------#
#     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文件中定义的函数:

代码语言:javascript
复制
#ifndef MATHH
#define MATHH

int addNums(int a, int b);

#endif

但奇怪的是,QtCreator能够遵循对头文件的引用。

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

任何关于如何改进的帮助或建议都是值得感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-13 03:58:04

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

代码语言:javascript
复制
stalib.commands = make -C../thirdparty/stalib CROSS_COMPILE=1

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

代码语言:javascript
复制
extern "C" {
    #include "math.h"
}

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58824849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档