## Question Authors' Forum

### C++ program question with user defined header files

by a Salem -
Number of replies: 5

Hello,

I want to create a question on CodeRunner of type "C++ program". I have done it before for programs where students need to copy and past their code (normally main.cpp type of file ).  Now, I am preparing a question where students need to submit the cpp file that includes the main function of the program in addition to two (2) other files for a class that will be included and used in the main function file. Basically a user-defined class as header file (e.g. "classX.h" for the class declaration and "classX.cpp" for the class implementation.

I tired to include the two files in the "sample answer attachment", but apparently it is not the correct way to do it as when I save, it shows list of errors as :

/usr/bin/ld: __tester__.cpp:(.text+0x26e): undefined reference to .....,>
How can I add the user-defined class header file and its implementation (cpp file) to the question. Students also will have to include and therefore attach similar files within their submission.
I would really appreciate your help regarding that.
Thanks in advance.

In reply to a Salem

### Re: C++ program question with user defined header files

by Richard Lobb -
You should be able to upload the files into the Support Files section of the question - all files there are loaded into the temporary working directory on the Jobe server in which the source file is compiled and run. However, I'm not quite sure I understand the bit in your posting: "Students also will have to include and therefore attach similar files within their submission". If you provide a support file, it will be write protected so students can't overwrite it with an identically named file in their own submission. Can you clarify, please?
In reply to Richard Lobb

### Re: C++ program question with user defined header files

by a Salem -
Thank you for your reply. it was really helpful and to the point. I managed to do that. I provided the sample answer main.cpp file's content to the Answer section and uploaded, to the Support Files section, a user-defined header file that I am using in my main file through #include"classX.h" and the same for the corresponding cpp file for classX implementation.  Now, it runs perfectly fine. In addition to that, I also uploaded, to the Support Files section,  some text files that the main program will read data from when provided the text file name.

So now I believe I just need to allow the attachment (from the Attachment options section) with the number of files that student will have to upload while submitting their answer (which will be another .h and .cpp files that they will be using in their main.) and those two files has to be named any thing other than classX.h and classX.cpp.  Is that right?

Thanks

In reply to a Salem

### Re: C++ program question with user defined header files

by Richard Lobb -
Good to hear that things are coming right. All sounds good. I assume you've figured out how to compile multiple files? It might help to know that the compile command on the Jobe server is (in PHP):

$cmd = "g++ " . implode(' ',$compileargs) . " -o $execFileName$src " . implode(' ', $linkargs); where •$compileargs is by default ['-Wall', '-Werror']
• $linkargs is by default ['-lm'] •$src is the name of the source file containing the code from the answer box
• $execFileName is the basename extracted from$src.

You can override the compileargs and linkargs in the question's Advanced Settings > Sandbox > Parameters (once customisation is turned on). If you're going to allow students to upload any cpp filename they like, you'll have to get a "*.cpp" in there, somewhere.

Alternatively (and this is how we usually work) you can use a question scripted in Python, as described here. That gives you total control over the compiling, linking and execution.

In reply to Richard Lobb

### Re: C++ program question with user defined header files

by a Salem -
Thanks for the reply. Yes, things are coming right. I uploaded the header file and its corresponding cpp file to the "Support Files" section and the main file is in the "Answer" section and when I verify, it works perfectly fine. The student will have to do the same as well.

And thanks for the additional info. but as far as I can see, I don't have to use them, is that right or am I missing something? As for now, I didn't have to change any of those settings, and I don't even turned on the customisation for the question, and I assume that it will work fine when I post the question and students start submitting their answers because I tried it with a student role (account) and it works fine.

Thanks again.
In reply to a Salem

### Re: C++ program question with user defined header files

by Richard Lobb -
The normal pattern for C and C++ projects is that the main program #includes the .h files but not the .cpp files. The latter then need to be compiled separately and all then get linked to an executable by 'ld' (usually invoked by the compiler itself). A single command like "g++ *.cpp -o prog" will do all that for you, but that's not the command that CodeRunner uses: it just compiles the one explicitly-named program. So you would need to modify CodeRunners compile-and-link command.

However, if you tell the students to #include both the .h and any .cpp files in the main program, then you don't need to separately compile the .cpp files. It's unorthodox, and rather defeats the point of having .h files but it works OK. It sounds like that's what you must be doing.