c - Qt 静态库子模块构建
问题描述
我想自动构建第三方库并将它们包含在我的 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
任何有关如何改进的帮助或建议表示赞赏。
解决方案
如果要自动构建库,则需要在 .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
推荐阅读
- node.js - 离子文件传输太大?
- pandas - Pandas Group By + Cumsum 分类变量
- reactjs - 如何访问 React 组件中嵌套的 Redux 状态?
- r - gmm_rolling 估计中方差膨胀因子的错误
- python - 使用 python cgi 创建 JSON 只返回一行
- dart - 在 Flutter 中更改 TextField 装饰 onFocus
- javascript - 将图像添加到每个图像之间的时间间隔的类
- python - 如何在 tkinter 中将图像设置为背景?
- python - QWebEngineUrlRequestInterceptor 不工作
- google-apps-script - 如果用户删除了附加组件的权限,则停止可安装触发器