0

I am working on an assignment where I have main.cpp,lego.cpp, and lego.h. My program currently runs fine and gives out the desired output. My issue is that to submit, I must have everything in a single cpp file. Would appreciate if someone could give a simple example. (I want everything in main.cpp)

Merge C++ files into a single source file

i found this link that is similar but I cant grasp the example

wetmoney
  • 59
  • 10
  • 1
    It should generally be fine to copy the majority of the contents of `lego.h` and `lego.cpp` above everything in `main`, and then remove those two files from your project. – JohnFilleau Jun 13 '20 at 23:25
  • 1
    This is an unusual question. Normally it's "Everything was find, and then I split my code up into different files..." Thanks for mixing it up for us. – user4581301 Jun 13 '20 at 23:34
  • @JohnFilleau i tried this and got some linking errors. Do i have to place lego.cpp inside of lego.h? – wetmoney Jun 13 '20 at 23:35
  • 1
    @user4581301 I was about to make a similar comment. It's unusual to have someone who's made an effort to split everything up properly and is then forced to concatenate it :-) – Ted Lyngmo Jun 13 '20 at 23:36
  • @user4581301 what was the point of commenting – wetmoney Jun 13 '20 at 23:36
  • Idea: Run your compiler's pre-processor only and save the output. – Ted Lyngmo Jun 13 '20 at 23:37
  • @wetmoney It was a compliment. – Ted Lyngmo Jun 13 '20 at 23:37
  • @user4581301 my bad, I misread. I read "Thanks for messing it up for us" and didnt understand why someone was attacking me for my question lol – wetmoney Jun 13 '20 at 23:41
  • @TedLyngmo Regarding your idea: What if the program includes standard headers? – eerorika Jun 13 '20 at 23:43
  • @eerorika That might perhaps be troublesome. If this is something one wants to do repeatedly, scripting it wouldn't be too hard. Perhaps the pre-processor-idea was not so good. – Ted Lyngmo Jun 13 '20 at 23:47

3 Answers3

1

if anyone has this problem I just fixed it by doing this

class lego {things in class};

lego::functionA(){}

lego::functionB(){}

int main(){};

make sure that you delete any other cpp files that are in your source folder and remove any header files from main. Was what was causing my issue.

wetmoney
  • 59
  • 10
0

Combining two cpp files into one cpp file

On a POSIX system, you can use the following command:

cat main.cpp lego.cpp > combined.cpp

On windows, use type instead of cat.

This trivial approach works for most programs, and very likely for a simple assignment. But this can have problems with more complicated programs in particular those that rely heavily on pre-processor.

eerorika
  • 232,697
  • 12
  • 197
  • 326
-1

using the command prompt by combining the files. you need to put the files that you need to combine in a single folder and using the command line (windows + r, on windows 10) run commands that access the directory in which the files are.

Enter the command copy/b *.cpp combined.cpp That will save the two cpp files into one file combined.cpp. Then access the combined file combined.cpp in the same directory. This might help you too https://www.youtube.com/watch?v=0atEx7EngoE

  • 1
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) –  Jul 17 '20 at 09:47