CMake GCC 在包含的头文件中找不到 curl 结构
CMake GCC can't find curl struct in included header file
我正在尝试使用 CMake 创建一个使用 libcurl 的库,但 GCC 始终无法找到某些结构和定义,即使它们被包含在内。我安装了 curl
、libssl-dev
和 libcurl4-openssl-dev
。我能够使用 gcc libcurl-out.c -lcurl
.
编译由 curl http://example.com --libcurl libcurl-out.c
生成的独立示例程序
这是我的项目结构:
OpenDropC/
├── include/
│ ├── browser.h
│ ├── client.h
│ ├── config.h
│ └── server.h
├── src/
│ ├── browser.c
│ ├── client.c
│ ├── config_private.h
│ ├── config.c
│ ├── server.c
│ ├── utils.c
│ └── utils.h
└── CMakeLists.txt
config.c
包括 config_private.h
,其中包括 <curl/curl.h>
。将 curl include 直接放在 config.c
中并不能解决问题。
这是我的 CMakeLists.txt
:
cmake_minimum_required(VERSION 3.18)
project(OpenDropC VERSION 0.0.1 DESCRIPTION "Implementation of OpenDrop in C for Linux")
include(CTest)
enable_testing()
add_library(OpenDropC SHARED
src/browser.c
src/client.c
src/server.c
src/config.c
src/utils.c
)
# set_target_properties(OpenDropC PROPERTIES PUBLIC_HEADER include/browser.h)
target_link_libraries(OpenDropC PRIVATE avahi-common avahi-client curl ssl)
这是我得到的编译器错误:
[main] Building folder: OpenDropC
[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /home/parallels/Code/OpenDropC/build --config Debug --target all -j 4 --
[build] [4/8 12% :: 0.043] Building C object CMakeFiles/OpenDropC.dir/src/server.c.o
[build] [5/8 25% :: 0.063] Building C object CMakeFiles/OpenDropC.dir/src/browser.c.o
[build] [6/8 37% :: 0.072] Building C object CMakeFiles/OpenDropC.dir/src/client.c.o
[build] [6/8 50% :: 0.080] Building C object CMakeFiles/OpenDropC.dir/src/utils.c.o
[build] [6/8 62% :: 0.088] Building C object CMakeFiles/OpenDropC.dir/src/config.c.o
[build] FAILED: CMakeFiles/OpenDropC.dir/src/config.c.o
[build] /bin/gcc-9 -DOpenDropC_EXPORTS -g -fPIC -MD -MT CMakeFiles/OpenDropC.dir/src/config.c.o -MF CMakeFiles/OpenDropC.dir/src/config.c.o.d -o CMakeFiles/OpenDropC.dir/src/config.c.o -c /home/parallels/Code/OpenDropC/src/config.c
[build] /home/parallels/Code/OpenDropC/src/config.c: In function ‘opendrop_config_new’:
[build] /home/parallels/Code/OpenDropC/src/config.c:42:70: error: invalid application of ‘sizeof’ to incomplete type ‘struct curl_blob’
[build] 42 | if (!(config_unwrap->root_ca = (struct curl_blob*) malloc(sizeof(struct curl_blob)))) {
[build] | ^~~~~~
[build] /home/parallels/Code/OpenDropC/src/config.c:48:33: error: dereferencing pointer to incomplete type ‘struct curl_blob’
[build] 48 | if (!(config_unwrap->root_ca->data = malloc(root_ca_len))) {
[build] | ^~
[build] /home/parallels/Code/OpenDropC/src/config.c:56:37: error: ‘CURL_BLOB_NOCOPY’ undeclared (first use in this function)
[build] 56 | config_unwrap->root_ca->flags = CURL_BLOB_NOCOPY;
[build] | ^~~~~~~~~~~~~~~~
[build] /home/parallels/Code/OpenDropC/src/config.c:56:37: note: each undeclared identifier is reported only once for each function it appears in
[build] /home/parallels/Code/OpenDropC/src/config.c: In function ‘opendrop_config_set_record_data’:
[build] /home/parallels/Code/OpenDropC/src/config.c:280:71: error: invalid application of ‘sizeof’ to incomplete type ‘struct curl_blob’
[build] 280 | if (!(config->record_data = (struct curl_blob*) malloc(sizeof(struct curl_blob)))) {
[build] | ^~~~~~
[build] /home/parallels/Code/OpenDropC/src/config.c:284:38: error: ‘CURL_BLOB_NOCOPY’ undeclared (first use in this function)
[build] 284 | config->record_data->flags = CURL_BLOB_NOCOPY;
[build] | ^~~~~~~~~~~~~~~~
[build] [6/8 75% :: 0.097] Building C object CMakeFiles/OpenDropCTest.dir/test/main.c.o
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1
运行 curl-config --cflags
returns 一个空字符串,但我尝试显式地将目录包含在 curl 文件中,但没有任何改变。我也试过 this answer 没有改变。其他库编译得很好,所以这似乎是一个 libcurl 问题。尝试将 VCPKG 专门用于 curl 确实 有效,但使用它会使我的项目过于臃肿。
我通过从源代码安装最新版本的 libcurl 解决了这个问题。 Ubuntu 20.04 的 libcurl 版本似乎有问题。
我正在尝试使用 CMake 创建一个使用 libcurl 的库,但 GCC 始终无法找到某些结构和定义,即使它们被包含在内。我安装了 curl
、libssl-dev
和 libcurl4-openssl-dev
。我能够使用 gcc libcurl-out.c -lcurl
.
curl http://example.com --libcurl libcurl-out.c
生成的独立示例程序
这是我的项目结构:
OpenDropC/
├── include/
│ ├── browser.h
│ ├── client.h
│ ├── config.h
│ └── server.h
├── src/
│ ├── browser.c
│ ├── client.c
│ ├── config_private.h
│ ├── config.c
│ ├── server.c
│ ├── utils.c
│ └── utils.h
└── CMakeLists.txt
config.c
包括 config_private.h
,其中包括 <curl/curl.h>
。将 curl include 直接放在 config.c
中并不能解决问题。
这是我的 CMakeLists.txt
:
cmake_minimum_required(VERSION 3.18)
project(OpenDropC VERSION 0.0.1 DESCRIPTION "Implementation of OpenDrop in C for Linux")
include(CTest)
enable_testing()
add_library(OpenDropC SHARED
src/browser.c
src/client.c
src/server.c
src/config.c
src/utils.c
)
# set_target_properties(OpenDropC PROPERTIES PUBLIC_HEADER include/browser.h)
target_link_libraries(OpenDropC PRIVATE avahi-common avahi-client curl ssl)
这是我得到的编译器错误:
[main] Building folder: OpenDropC
[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /home/parallels/Code/OpenDropC/build --config Debug --target all -j 4 --
[build] [4/8 12% :: 0.043] Building C object CMakeFiles/OpenDropC.dir/src/server.c.o
[build] [5/8 25% :: 0.063] Building C object CMakeFiles/OpenDropC.dir/src/browser.c.o
[build] [6/8 37% :: 0.072] Building C object CMakeFiles/OpenDropC.dir/src/client.c.o
[build] [6/8 50% :: 0.080] Building C object CMakeFiles/OpenDropC.dir/src/utils.c.o
[build] [6/8 62% :: 0.088] Building C object CMakeFiles/OpenDropC.dir/src/config.c.o
[build] FAILED: CMakeFiles/OpenDropC.dir/src/config.c.o
[build] /bin/gcc-9 -DOpenDropC_EXPORTS -g -fPIC -MD -MT CMakeFiles/OpenDropC.dir/src/config.c.o -MF CMakeFiles/OpenDropC.dir/src/config.c.o.d -o CMakeFiles/OpenDropC.dir/src/config.c.o -c /home/parallels/Code/OpenDropC/src/config.c
[build] /home/parallels/Code/OpenDropC/src/config.c: In function ‘opendrop_config_new’:
[build] /home/parallels/Code/OpenDropC/src/config.c:42:70: error: invalid application of ‘sizeof’ to incomplete type ‘struct curl_blob’
[build] 42 | if (!(config_unwrap->root_ca = (struct curl_blob*) malloc(sizeof(struct curl_blob)))) {
[build] | ^~~~~~
[build] /home/parallels/Code/OpenDropC/src/config.c:48:33: error: dereferencing pointer to incomplete type ‘struct curl_blob’
[build] 48 | if (!(config_unwrap->root_ca->data = malloc(root_ca_len))) {
[build] | ^~
[build] /home/parallels/Code/OpenDropC/src/config.c:56:37: error: ‘CURL_BLOB_NOCOPY’ undeclared (first use in this function)
[build] 56 | config_unwrap->root_ca->flags = CURL_BLOB_NOCOPY;
[build] | ^~~~~~~~~~~~~~~~
[build] /home/parallels/Code/OpenDropC/src/config.c:56:37: note: each undeclared identifier is reported only once for each function it appears in
[build] /home/parallels/Code/OpenDropC/src/config.c: In function ‘opendrop_config_set_record_data’:
[build] /home/parallels/Code/OpenDropC/src/config.c:280:71: error: invalid application of ‘sizeof’ to incomplete type ‘struct curl_blob’
[build] 280 | if (!(config->record_data = (struct curl_blob*) malloc(sizeof(struct curl_blob)))) {
[build] | ^~~~~~
[build] /home/parallels/Code/OpenDropC/src/config.c:284:38: error: ‘CURL_BLOB_NOCOPY’ undeclared (first use in this function)
[build] 284 | config->record_data->flags = CURL_BLOB_NOCOPY;
[build] | ^~~~~~~~~~~~~~~~
[build] [6/8 75% :: 0.097] Building C object CMakeFiles/OpenDropCTest.dir/test/main.c.o
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1
运行 curl-config --cflags
returns 一个空字符串,但我尝试显式地将目录包含在 curl 文件中,但没有任何改变。我也试过 this answer 没有改变。其他库编译得很好,所以这似乎是一个 libcurl 问题。尝试将 VCPKG 专门用于 curl 确实 有效,但使用它会使我的项目过于臃肿。
我通过从源代码安装最新版本的 libcurl 解决了这个问题。 Ubuntu 20.04 的 libcurl 版本似乎有问题。