4

Taking Boost as an example, why am I able to link against boost::filesystem from my C++ program, when C++ does not have a stable ABI.

I have Boost installed in my system, and a toy program is able to link using -lboost_filesystem, even though boost::filesystem exposes a C++ API ( no extern 'C' ).

So, is it possible to create C++ shared libraries that can be linked by various compiler versions? How does Boost achieve this without "extern C"?

I tried:

#include <iostream>
#include <boost/filesystem.hpp>

namespace fs = boost::filesystem;

int main() {
    // Replace "/path/to/directory" with the path to the directory you want to list
    std::string directory_path = "/path/to/directory";

    try {
        // Check if the given path exists and is a directory
        if (fs::exists(directory_path) && fs::is_directory(directory_path)) {
            std::cout << "Listing files in directory: " << directory_path << std::endl;

            // Iterate through the files in the directory
            for (const auto& entry : fs::directory_iterator(directory_path)) {
                std::cout << entry.path() << std::endl;
            }
        } else {
            std::cerr << "Error: Invalid directory path." << std::endl;
            return 1;
        }
    } catch (const fs::filesystem_error& ex) {
        std::cerr << "Error: " << ex.what() << std::endl;
        return 1;
    }

    return 0;
}

g++ -o fs fs.cpp -I/usr/include/boost -I/usr/include/boost/filesystem -I/usr/include/boost/system -lboost_system -lboost_filesystem -std=c++14

Expectation: Should get linking errors as C++ does not have a stable ABI

Got: Compilation Succeeded.

agrawal-d
  • 190
  • 1
  • 8
  • 1
    Mangling aside, how is boost able to account for the same memory layout of data structures across compiler versions/optimizations? – agrawal-d Aug 01 '23 at 04:51
  • 4
    It's possible to link against a shared library with a "C++ API" if the library and the program are built with compilers that support compatible ABIs. g++ and clang follow the same scheme. While the ABI *can* change between compiler versions, it doesn't change particularly often - for a given ABI, there will be a significant number of compatible compiler versions. It is still possible to break ABI compatibility between versions - in which case, there will need to be effort to re-establish compatibility (including rebuilding libraries and programs from source using compatible compilers). – Peter Aug 01 '23 at 04:57
  • 1
    Why do you think the ABI is not stable (in time)? It's just different on some platforms. – HolyBlackCat Aug 01 '23 at 05:59
  • Whilst the c++ standard doesn't define an ABI the various platforms do define an ABI with varying levels of stability – Alan Birtles Aug 01 '23 at 06:48
  • 3
    "Expectation: Should get linking errors as C++ does not have a stable ABI": *non sequitur.* You *might* get linking errors for that reason, but you have no rational reason for *expecting* it. – user207421 Aug 01 '23 at 08:11

1 Answers1

6

I'm not sure about ABI per se, but the real crux of linking a C++ library built by one compiler with a program compiled with another compiler would rely on both compilers sharing the same name mangling schema.

And indeed, g++ and clang++ work the same and have had the same schema for years.

But as this question and answer reveal, trying to do something similar by compiling a library on Windows with MinGW and expecting it to link with Visual Studio, doesn't work. Probably not possible unless something has changed in recent years.

selbie
  • 100,020
  • 15
  • 103
  • 173
  • 2
    Clang is a chameleon - if you use the Windows version of clang++, it matches Visual Studio. – BoP Aug 01 '23 at 09:08