在Qt中使用mongoose.c、mongoose.h报错汇总

采用Qt5,QtCreator编辑器,MinGW的编译器,CMakeList.txt进行编译测试的,不是pro。

window环境(linux的话问题可能没有)

1、错误1

错误信息如下:一大堆未定义引用
main.cpp:26: error: undefined reference to `mg_json_get_num'

解决方法

CMakeLists.txt 需要设置C语言 项目语言只设置了 CXX ,没有包含 C

project(http_test LANGUAGES C CXX)

2、错误2

mongoose.c:3778: error: undefined reference to `__imp_WSAStartup'

解决方法

  • mongoose.h 中有 #pragma comment(lib, "ws2_32.lib") ,但这只在 MSVC 编译器下有效

  • MinGW 编译器不支持 #pragma comment ,需要在 CMakeLists.txt 中显式链接

现在重新构建项目即可。

target_link_libraries(http_test Qt${QT_VERSION_MAJOR}::Core ws2_32)