How to fix the error of could not build C module 'XXX'

There is a third party C library(XXX) that I put it in my library(YYY) as a git submodule.

Package.swift
Sources/XXX/...
Sources/YYY/YYY.swift

The Package.swift is

import PackageDescription

let package = Package(
    name: "YYY",
    products: [
        .library(name: "YYY", targets: ["YYY"]),
    ],
    dependencies: [],
    targets: [
        .target(
                name: "XXX",
                dependencies: [],
                exclude: [
                    "src/CMakeLists.txt",
                    "CMakeLists.txt",
                ]),
        .target(
            name: "YYY",
            dependencies: ["XXX"])
    ]
)

I get an error: "could not build C module 'XXX' import XXX" when I run swift build. But if I remove all import XXX in YYY.swift, it can be build successfully.

The error detail:

C:\Users\xuyue\CLionProjects\YYY\Sources\XXX\include\xxx/x.h:27:16: note: unguarded header; consider using #ifdef guards or #pragma once
typedef struct xInfo {
^
:24:10: note: in file included from :24:
#include "C:\Users\xuyue\CLionProjects\YYY\Sources\XXX\include\xxx\x.h"
^
C:\Users\xuyue\CLionProjects\YYY\Sources\XXX\include\xxx/x.h:37:3: error: typedef redefinition with different types ('struct (anonymous struct at C:\Users\xuyue\CLionProjects\YYY\Sources\XXX\include\xxx/x.h:27:16)' vs 'struct xInfo')
} xInfo;
^
C:\Users\xuyue\CLionProjects\YYY\Sources\XXX\include\xxx/xxx.h:121:10: note: 'C:\Users\xuyue\CLionProjects\YYY\Sources\XXX\include\xxx/x.h' included multiple times, additional include site in header from module 'XXX'
#include "x.h"

C:\Users\xuyue\CLionProjects\YYY.build\x86_64-unknown-windows-msvc\debug\XXX.build\module.modulemap:1:8: note: XXX defined here
module XXX {

:0: error: too many errors emitted, stopping now

It seems to be due to repeated import, how to correct it?

Is anybody can help me? :joy:

Have a look at the first part of the error message:

In C land, it is generally upon the header to make sure it doesn't declare duplicate symbols when included multiple times, which I guess the header at hand doesn't do.

This could be fixed in the original C library, or it might be possible to address in a shim header as well.

Terms of Service

Privacy Policy

Cookie Policy