r/cpp_questions • • 21d ago

OPEN Any sources to learn how C++ source projects get lowered from source code to intermediate representation and eventually to code gen part.

Can someone help me out....I'm not able to find any proper sources to follow how c++ projects get lowered from sources codes with headers and archive files to intermediate representation and then object files and shared files and linking like how do they work and their internals ....just trying to learn about them.

3 Upvotes

5 comments sorted by

4

u/trichotomy00 21d ago

are you asking how compilers work?

-1

u/Dawn232023 21d ago

Yeah more or less. I understand how to compile a single source file but when I moved to big cpp projects take llvm test suite for an example they i believe need something like a 'recipe' like a cmake file with all the flags and dependencies listed now that part is still okay but then linkers have different rules of linking and discarding based on set criterion and that part or the portion is a bit difficult for me to gulp down. Now I searched youtube for any videos on that but was defeated and don't know if following llvm documentation for this is the ryt call or are there any other ways to know

1

u/Discordbothost 20d ago

LLD's ELF archive docs cover that linker question: https://lld.llvm.org/ELF/warn_backrefs.html

try a tiny object + static archive example with GNU ld and LLD. they can resolve the same archive ordering differently, which is hard to spot in a large CMake build.

1

u/HeeTrouse51847 21d ago

the point of cmake is to have a higher level build tool so you DONT have to deal with platform or compiler specific stuff. In a perfect world, your entire C++ project is completely defined by your source files and your CMakeLists.txt and solely based on those can be built on any platform by just running

cmake -B build

cmake --build build

In reality, you still often need to pass specific compiler options or handle OS specific cases separately. Adding Third party libraries to your project also isnt something that has a single "best solution". You can install them manually and make CMake find them. Or you can use "fetch content" (a cmake mechanism to pull git repos of 3rd party libs) or use a package manager like vcpkg or conan and connect them into your cmake project. (I recommend conan)

I would recommend starting to learn how to setup your C++ projects with CMake and then have a look at how to use the Conan package manager.

You mentioned you want to use the LLVM test suite, may I ask, why?

I have never used it before or heard of it, but quickly glancing over it, my understanding is this:

It's not a regular C++ library, it's a project to test your local LLVM installation. To me, it does not seem trivial.

If you are a beginner and just want a unit test library, use Google Test or Catch2 instead.

0

u/davidc538 20d ago

If you know some assembly, Crafting Interpreters is a good book that helps bridge the gap.