Two implementations of one file name in different directories share one object: the link fails, or a debug build has one file's code twice and the other's not at all #50

Closed
opened 2026-11-19 05:21:12 +00:00 by jorijnvdgraaf · 0 comments

Summary

An implementation's object is named from its file's name alone, <build dir>/<name>_impl.o. So a/Thing.cpp and b/Thing.cpp in one configuration are compiled into the same object, each over the other, and the link names that object twice.

A release build then fails at the link, with a message that points at neither source. A debug build of two such files that define nothing another file names (files that register themselves when the program starts, say) succeeds without a word, and the program has one file's code twice and the other's not at all. Which of the two it is differs from run to run, and need not be the file that was changed.

Reproduced with the stable Flatpak, 6.3.0 (flatpak run net.catcrafts.CrafterBuild//stable, written crafter-build below). Line numbers refer to c929dce (6.4.0); each cited line is the same text in 6.3.0.

Met in a library that keeps one source file per method of an interface, <package>/<Interface>/<method>.cpp: every interface has an Interface.cpp, and the same method name occurs under many interfaces.

Reproduce

Two fixtures of three sources each, main.cpp, a/Thing.cpp and b/Thing.cpp (below).

loud: each Thing.cpp defines a function that main calls. crafter-build:

[3/5] Compiling Thing.cpp
[4/5] Compiling main.cpp
[5/5] Compiling Thing.cpp
[6/6] Linking names
ld.lld: error: duplicate symbol: B()
>>> defined in ./build/names-x86_64-unknown-linux-gnu-native-native-fd6519de5e74e7e1/Thing_impl.o
>>> defined in ./build/names-x86_64-unknown-linux-gnu-native-native-fd6519de5e74e7e1/Thing_impl.o
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

A() is missing from the link and is not mentioned. crafter-build --debug fails the same way, in that run on the other file:

ld.lld: error: duplicate symbol: A()
>>> defined at Thing.cpp:1 (a/Thing.cpp:1)
>>>            ./build/names-x86_64-unknown-linux-gnu-native-native-c38576cb013569da/Thing_impl.o:(A())
>>> defined at Thing.cpp:1 (a/Thing.cpp:1)
>>>            ./build/names-x86_64-unknown-linux-gnu-native-native-c38576cb013569da/Thing_impl.o:(.text+0x0)

silent: each Thing.cpp defines nothing another file names. A static object in an anonymous namespace registers it when the program starts. crafter-build fails, since a release build's objects are bitcode (implementations/Crafter.Build-Clang.cpp:468):

ld.lld: error: Expected at most one ThinLTO module per bitcode file

crafter-build --debug succeeds. The build, and then the program:

[3/5] Compiling main.cpp
[4/5] Compiling Thing.cpp
[5/5] Compiling Thing.cpp
[6/6] Linking names

b/Thing.cpp registers
b/Thing.cpp registers
registered: 2

After touch b/Thing.cpp, crafter-build --debug compiles both again, and the program has a's code twice:

[3/4] Compiling Thing.cpp
[4/4] Compiling Thing.cpp
[5/5] Linking names

a/Thing.cpp registers
a/Thing.cpp registers
registered: 2

After touch a/Thing.cpp and another build it had b's twice. In an earlier run of the same steps the outcomes were the other way about, and one of the rebuilds compiled only one of the two files.

Fixtures

project.cpp, the same for both:

import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;

extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> args) {
    Configuration cfg;
    cfg.path = "./";
    cfg.name = "names";
    cfg.outputName = "names";
    ApplyStandardArgs(cfg, args);
    cfg.type = ConfigurationType::Executable;
    {
        std::array<fs::path, 0> ifaces = {};
        std::array<fs::path, 3> impls = { "main", "a/Thing", "b/Thing" };
        cfg.GetInterfacesAndImplementations(ifaces, impls);
    }
    return cfg;
}

loud/main.cpp

int A();
int B();
int main() { return A() + B() == 3 ? 0 : 1; }

loud/a/Thing.cpp

int A() { return 1; }

loud/b/Thing.cpp

int B() { return 2; }

silent/main.cpp

#include <stdio.h>
int Registered;
int main() {
    printf("registered: %d\n", Registered);
    return 0;
}

silent/a/Thing.cpp, and silent/b/Thing.cpp with b for a:

#include <stdio.h>
extern int Registered;
namespace {
    struct Registers {
        Registers() {
            puts("a/Thing.cpp registers");
            ++Registered;
        }
    } itself;
}

Where

  • implementations/Crafter.Build-Implementation.cpp:17: Implementation::Check takes the object to be buildDir/path.filename() with _impl.o, and :22 reads the dependency file beside it.
  • implementations/Crafter.Build-Implementation.cpp:53: Implementation::Schedule compiles to the same name, -MF {2}_impl.o.d … -o {2}_impl.o.
  • implementations/Crafter.Build-Clang.cpp:1384: the link's list of objects is made from implementation.path.filename(), an entry for each implementation, so the shared object is on it twice.
  • implementations/Crafter.Build-Clang.cpp:1502: the check whether to link again, by the same name.
  • C and CUDA sources are named the same way, with _source.o (implementations/Crafter.Build-Clang.cpp:1178 and :1192, and :1515 for linking again). Read there and not run: a .c and a .cu of one name would share an object even in one directory.

The directory a source lies in is in none of these. The two implementations share the dependency file as well, so each is checked for staleness against the source and the headers of whichever was compiled last. Not run: a change to a header that only the other one includes.

A module interface's BMI and object are named from the file's name too (implementations/Crafter.Build-Interface.cpp:45, :48). The BMIs are found through -fprebuilt-module-path (implementations/Crafter.Build-Clang.cpp:337), which looks for a file of the module's name, so two interface files of one name would have to be one module. That is not this issue.

Proposal

Name an implementation's object and its dependency file from the source's path relative to the project, and a C or CUDA source's likewise: a directory under the build directory for each source directory, or the path made part of the one name.

What that changes beside the fix:

  • Every implementation's object gets a new name, so existing build directories compile their implementations once more, and the objects under the old names stay there, unused.
  • The link command gets longer by each source's directory. It is run through popen, as one argument to the shell (implementations/Crafter.Build-Platform.cpp:708), and one argument may be 131,071 bytes here (Linux, x86_64): with 131,072, starting a program fails with "Argument list too long". For a library of 15 implementations the recorded link command (<name>.link) is 2,731 bytes, 1,498 of them the implementations' objects: 100 apiece on average, 69 of that the build directory with its slash. At that rate about 1,300 objects fit, and fewer with directories in their names. A response file for the objects would take that limit away. The link uses none today.
  • The line Compiling Thing.cpp could carry the path, so that the two can be told apart.

If the names are to stay as they are, the least that would help is to refuse a configuration in which two implementations, or two C sources, have the same file name, and to name both. That takes away the link error that points nowhere and the silent debug build. A layout like the one above still could not be built.

Not looked at

Windows and WebAssembly targets; a source named by a path with .. in it; what becomes of the objects of a file that is no longer in the project.

## Summary An implementation's object is named from its file's name alone, `<build dir>/<name>_impl.o`. So `a/Thing.cpp` and `b/Thing.cpp` in one configuration are compiled into the same object, each over the other, and the link names that object twice. A release build then fails at the link, with a message that points at neither source. A debug build of two such files that define nothing another file names (files that register themselves when the program starts, say) succeeds without a word, and the program has one file's code twice and the other's not at all. Which of the two it is differs from run to run, and need not be the file that was changed. Reproduced with the stable Flatpak, 6.3.0 (`flatpak run net.catcrafts.CrafterBuild//stable`, written `crafter-build` below). Line numbers refer to `c929dce` (6.4.0); each cited line is the same text in 6.3.0. Met in a library that keeps one source file per method of an interface, `<package>/<Interface>/<method>.cpp`: every interface has an `Interface.cpp`, and the same method name occurs under many interfaces. ## Reproduce Two fixtures of three sources each, `main.cpp`, `a/Thing.cpp` and `b/Thing.cpp` (below). **`loud`**: each `Thing.cpp` defines a function that `main` calls. `crafter-build`: ``` [3/5] Compiling Thing.cpp [4/5] Compiling main.cpp [5/5] Compiling Thing.cpp [6/6] Linking names ld.lld: error: duplicate symbol: B() >>> defined in ./build/names-x86_64-unknown-linux-gnu-native-native-fd6519de5e74e7e1/Thing_impl.o >>> defined in ./build/names-x86_64-unknown-linux-gnu-native-native-fd6519de5e74e7e1/Thing_impl.o clang++: error: linker command failed with exit code 1 (use -v to see invocation) ``` `A()` is missing from the link and is not mentioned. `crafter-build --debug` fails the same way, in that run on the other file: ``` ld.lld: error: duplicate symbol: A() >>> defined at Thing.cpp:1 (a/Thing.cpp:1) >>> ./build/names-x86_64-unknown-linux-gnu-native-native-c38576cb013569da/Thing_impl.o:(A()) >>> defined at Thing.cpp:1 (a/Thing.cpp:1) >>> ./build/names-x86_64-unknown-linux-gnu-native-native-c38576cb013569da/Thing_impl.o:(.text+0x0) ``` **`silent`**: each `Thing.cpp` defines nothing another file names. A static object in an anonymous namespace registers it when the program starts. `crafter-build` fails, since a release build's objects are bitcode (`implementations/Crafter.Build-Clang.cpp:468`): ``` ld.lld: error: Expected at most one ThinLTO module per bitcode file ``` `crafter-build --debug` succeeds. The build, and then the program: ``` [3/5] Compiling main.cpp [4/5] Compiling Thing.cpp [5/5] Compiling Thing.cpp [6/6] Linking names b/Thing.cpp registers b/Thing.cpp registers registered: 2 ``` After `touch b/Thing.cpp`, `crafter-build --debug` compiles both again, and the program has `a`'s code twice: ``` [3/4] Compiling Thing.cpp [4/4] Compiling Thing.cpp [5/5] Linking names a/Thing.cpp registers a/Thing.cpp registers registered: 2 ``` After `touch a/Thing.cpp` and another build it had `b`'s twice. In an earlier run of the same steps the outcomes were the other way about, and one of the rebuilds compiled only one of the two files. <details> <summary>Fixtures</summary> `project.cpp`, the same for both: ```cpp import std; import Crafter.Build; namespace fs = std::filesystem; using namespace Crafter; extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> args) { Configuration cfg; cfg.path = "./"; cfg.name = "names"; cfg.outputName = "names"; ApplyStandardArgs(cfg, args); cfg.type = ConfigurationType::Executable; { std::array<fs::path, 0> ifaces = {}; std::array<fs::path, 3> impls = { "main", "a/Thing", "b/Thing" }; cfg.GetInterfacesAndImplementations(ifaces, impls); } return cfg; } ``` `loud/main.cpp` ```cpp int A(); int B(); int main() { return A() + B() == 3 ? 0 : 1; } ``` `loud/a/Thing.cpp` ```cpp int A() { return 1; } ``` `loud/b/Thing.cpp` ```cpp int B() { return 2; } ``` `silent/main.cpp` ```cpp #include <stdio.h> int Registered; int main() { printf("registered: %d\n", Registered); return 0; } ``` `silent/a/Thing.cpp`, and `silent/b/Thing.cpp` with `b` for `a`: ```cpp #include <stdio.h> extern int Registered; namespace { struct Registers { Registers() { puts("a/Thing.cpp registers"); ++Registered; } } itself; } ``` </details> ## Where - `implementations/Crafter.Build-Implementation.cpp:17`: `Implementation::Check` takes the object to be `buildDir/path.filename()` with `_impl.o`, and `:22` reads the dependency file beside it. - `implementations/Crafter.Build-Implementation.cpp:53`: `Implementation::Schedule` compiles to the same name, `-MF {2}_impl.o.d … -o {2}_impl.o`. - `implementations/Crafter.Build-Clang.cpp:1384`: the link's list of objects is made from `implementation.path.filename()`, an entry for each implementation, so the shared object is on it twice. - `implementations/Crafter.Build-Clang.cpp:1502`: the check whether to link again, by the same name. - C and CUDA sources are named the same way, with `_source.o` (`implementations/Crafter.Build-Clang.cpp:1178` and `:1192`, and `:1515` for linking again). Read there and not run: a `.c` and a `.cu` of one name would share an object even in one directory. The directory a source lies in is in none of these. The two implementations share the dependency file as well, so each is checked for staleness against the source and the headers of whichever was compiled last. Not run: a change to a header that only the other one includes. A module interface's BMI and object are named from the file's name too (`implementations/Crafter.Build-Interface.cpp:45`, `:48`). The BMIs are found through `-fprebuilt-module-path` (`implementations/Crafter.Build-Clang.cpp:337`), which looks for a file of the module's name, so two interface files of one name would have to be one module. That is not this issue. ## Proposal Name an implementation's object and its dependency file from the source's path relative to the project, and a C or CUDA source's likewise: a directory under the build directory for each source directory, or the path made part of the one name. What that changes beside the fix: - Every implementation's object gets a new name, so existing build directories compile their implementations once more, and the objects under the old names stay there, unused. - The link command gets longer by each source's directory. It is run through `popen`, as one argument to the shell (`implementations/Crafter.Build-Platform.cpp:708`), and one argument may be 131,071 bytes here (Linux, x86_64): with 131,072, starting a program fails with "Argument list too long". For a library of 15 implementations the recorded link command (`<name>.link`) is 2,731 bytes, 1,498 of them the implementations' objects: 100 apiece on average, 69 of that the build directory with its slash. At that rate about 1,300 objects fit, and fewer with directories in their names. A response file for the objects would take that limit away. The link uses none today. - The line `Compiling Thing.cpp` could carry the path, so that the two can be told apart. If the names are to stay as they are, the least that would help is to refuse a configuration in which two implementations, or two C sources, have the same file name, and to name both. That takes away the link error that points nowhere and the silent debug build. A layout like the one above still could not be built. ## Not looked at Windows and WebAssembly targets; a source named by a path with `..` in it; what becomes of the objects of a file that is no longer in the project.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Catcrafts/Crafter.Build#50
No description provided.