Include external header file for C on Compiler Explorer

4.9k Views Asked by At

I want to run a C project on Compiler Explorer (godbolt.org) which includes some external header files. What is the procedure to include these third-party header files on there?

Screenshot of the project

Suppose I want to run this code which includes snipmath.h file. Also, snipmath.h header includes other lib files. How can I include all of them in godbolt? like we usually do in a offline compiler.

2

There are 2 best solutions below

0
On

You can use Compiler Explorer with Cmake and have multiple header and source files. You could even have some other files to read from (like *.ini). Try it here reading *.ini in Compiler Explorer

Note: the examples are with C++, but it must be straightforward to do it with C.

0
On

Remarkably, only 1 week before you posted the question this feature was added to compiler explorer. As of Nov 2021 it is reported to still have some rough edges, and not every combination of controls work - but starting from the simple template Matt delivered and following his instructions work for me:

  • go to compiler explorer

  • click "Add..." at the very top left and choose "Tree (IDE mode)".

  • Move the files you have open into "Included files" with the plus (you will have to give them names)

  • (For C++) tick "CMake" and then create a CMakeLists.txt with appropriate info it in

  • Ensure it says -DCMAKE_BUILD_TYPE=Debug in the box under

  • In the box under that choose the name of your target, e.g. "test"

  • You can then "Add new..." and make a compiler that will run CMake

Note this does not include uploading the headers, but rather pasting them in new CE editors. Also, judging by your screenshot - when you write 'other lib files' I assume you mean other headers. Lib files aren't supported.

If your project was a CMake one, you might have been able to upload it directly (someone did).