Using a header file on an online compiler

Is it possible for a C++ program run on an online C++ compiler to include a user-defined header file?

 
#include "myheader.h" 


I'm presently using http://cpp.sh but don't know if this feature is available there.

Otherwise, which online compiler provides this facility and how can I use it?

Thanks.
You can copy and paste your header file where you would have put #include "myheader.h".
Thanks for the reply, Borges.

The example you have provided is clear to me.

However, one question - How do I enter/upload a source file on coliru?

I have tried it, but I have found that, no matter on which button I click, I am unable to enter a program!

Thanks.
you can also add files on https://wandbox.org/ - just click the plus above the source code window.
> How do I enter/upload a source file on coliru?

I've always pasted the contents of the file in the browser window.

There is an API (not particularly well documented): eg. 'Compile example with curl' https://docs.google.com/document/d/18md3rLdgD9f5Wro3i7YYopJBFb_6MPCO8-0ihtxHoyM/edit
so it should be fairly easy to automate the process.
I have tried it. Internet Explorer was rendering coliru's home page incorrectly. The editor window didn't contain any default source code and I couldn't click on it. In fact there was no editor window at all.

However, I've tried opening coliru on Firefox now - and there it's perfectly OK.

So the problem was releted to incorrect rendering by Explorer.

Thanks.
I have tried the commands suggested, to invoke a program that includes a user-defined header, on coliru, but the program doesn't run.

Here are the links:

Header file : defines a couple of math functions:
http://coliru.stacked-crooked.com/a/c5bc0bb639d621db

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef MY_MATH_H_INCLUDED
#define MY_MATH_H_INCLUDED

int add(int a, int b)
{
    return a + b;
}

int multiply(int a, int b)
{
    return a * b;
}

#endif // MY_MATH_H_INCLUDED 


Note:
1) Here, I have included the function definitions also in the header file (rather than just the declarations). That's what I want to do.
2) When I click the "share" button, coliru tries to compile this header and issues an error saying main() function is not found. But it provides a link.


And, here's the client program that #includes the above header:
http://coliru.stacked-crooked.com/a/7d6f8953f1cd766f

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

#include "my_math.h"

using namespace std;


int main()
{
    cout << "add(2, 5) = "
         << add(2, 5) << endl;

    cout << "multiply(2, 5) = "
         << multiply(2, 5) << endl;
}



To compile and link this program to the header, I issued the following commands:

1
2
ln -s /Archive2/c5/bc0bb639d621db/main.cpp my_math.h #link to the header file 
clang++ -std=c++14 -stdlib=libc++ -O2 -Wall -Wextra -pedantic-errors -c main.cpp && echo ok


The output is as follows:

ln -s /Archive2/c5/bc0bb639d621db/main.cpp my_math.h #link to the header file 
clang++ -std=c++14 -stdlib=libc++ -O2 -Wall -Wextra -pedantic-errors -c main.cpp && echo ok
ok


The program output doesn't appear.

So I have the following queries:
1) The 1st command links the client-program to the header.
2) The 2nd command compiles the client program.
3) There doesn't seem to be a command to run the executable. Is that why the program's output doesn't appear?
4) How can we run the executable?
5) Why is the link command issued before the compile command?

Thanks.
> When I click the "share" button, coliru tries to compile this header
> and issues an error saying main() function is not found.

To prevent coliru from trying compile this header, use an empty command line (or just echo ok).
Ideally, add the inline specifier to the functions defined in the header.
http://coliru.stacked-crooked.com/a/37b74bb606edf81b


> There doesn't seem to be a command to run the executable. Is that why the program's output doesn't appear?

Yes. In fact, there is no command to create the executable. -c main.cpp only compiles the code.


> How can we run the executable?

First, create the executable (omit the -c option), and then issue a command to run it
http://coliru.stacked-crooked.com/a/1e3a9461f4b5754c
Absolutely correct, Borges. It works perfectly.

Thanks.
The scheme outlined above works well for multiple header files as well.

Eg: My test program #includes 2 header files:
1) The 1st header file my_math.h contains 2 functions: add() and mult()
2) The 2nd header file my_math2.h contains 2 functions: square() and cube()


Here's the tester: http://coliru.stacked-crooked.com/a/02bd0bf3adc36bf1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

#include "my_math.h"
#include "my_math2.h"

using namespace std;


int main()
{
    cout << "add(2, 5) = "
         << add(2, 5) << endl;

    cout << "multiply(2, 5) = "
         << multiply(2, 5) << endl;

    cout << "square(2) = "
         << square(2) << endl;

    cout << "cube(2) = "
         << cube(2) << endl;
}



And here are the commands to compile, link and run the tester:
1
2
3
4
5
6
7
8
9
10
11
#link to the header file my_math.h 
ln -s /Archive2/11/fffe8d6c138f08/main.cpp my_math.h 

#link to the header file my_math2.h
ln -s /Archive2/f3/90dc847bcd4916/main.cpp my_math2.h  

#compile and link to produce the executable 'my_math2_test'
clang++ -std=c++14 -stdlib=libc++ -O2 -Wall -Wextra -pedantic-errors main.cpp -o my_math2_test

#run 'my_math2_test' which is in the current directory
./my_math2_test
Topic archived. No new replies allowed.