Merge C++ files into a single source file

后端 未结 7 1614
慢半拍i
慢半拍i 2020-12-10 01:27

I have a c++ project with multiple source files and multiple header files. I want to submit my project for a programming contest which requires a single source file. Is the

相关标签:
7条回答
  • 2020-12-10 01:56

    The CIL utility is able to do this:

    $TIGRESS_HOME/cilly --merge -c x1.c -o x1.o
    $TIGRESS_HOME/cilly --merge -c x2.c -o x2.o
    $TIGRESS_HOME/cilly --merge -c x3.c -o x4.o
    $TIGRESS_HOME/cilly --merge  --keepmerged x1.o x2.o x3.o -o merged --mergedout=merged.c
    

    Usage example taken from here. Read the documentation about CIL and its shortcomings here. A binary distribution for Mac OS X and Linux is provided with Tigress.

    0 讨论(0)
  • 2020-12-10 02:02

    well, this is possiable, I have seen many project combine source files to single .h and .c/.cpp, such as sqlite

    but the code must have some limits, such as you should not have static global variable in one of your source codes.

    there may not have a generic tool for combine sources.you should write one base on your code.

    here is some examples

    gaclib source pack tool

    0 讨论(0)
  • 2020-12-10 02:07

    I don't know of a tool that combines .cpp files together but I would just zip all of the files up together and send them over as a gzip file.

    0 讨论(0)
  • 2020-12-10 02:08

    I also needed this for a coding contest. Codingame to be precise. So I wrote a quick JavaScript script to do the trick. You can find it here: https://www.npmjs.com/package/codingame-cpp-merge

    I used it in 1 live contest and one offline game and it never produced bad results. Feel free to suggest changes or make pull requests for it on github!

    0 讨论(0)
  • 2020-12-10 02:17

    I think the project with headers, sources files must be must nicer than the one with only one main file. Not only easier to work and read with but also they know you do good job at separating program's modules.

    Due to your solution, I provide this format and I think you have to do hand-work:

    // STL headers
    
    
    // --- prototype
    // monster.h
    
    // prince.h
    
    // --- implementation
    
    int main() { 
    // your main function
    return 0;
    }
    
    0 讨论(0)
  • 2020-12-10 02:19

    If you choose to send an individual file rather than a compressed archive, such as a tarball or a zip file, there are probably a few things you should consider.

    First, concatenate the files together as Thomas Matthews already mentioned. With a few changes, you can typically compile the one file. Remove the non-existent #include statements, such as the headers that have now been included.

    You will also have to concatenate these files in their respective dependency order. That is, if a.cpp needs a class declared in b.hpp, then you will most likely need to concatenate in the order Thomas Matthews listed.

    That being said, I think the best way to share code is via a public repository, such as GitHub.com or compressed archive.

    0 讨论(0)
提交回复
热议问题