Qt静态库子模块构建

问题描述 投票:0回答:1

我想自动构建第三方库并将其包含在我的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/myapphttps://github.com/faxe1008/stalib

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

c qt makefile
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.