C++ run project using standalone files, rather than a Code::Blocks project

Good afternoon!

I'm currently working on a final project to close my first semester of C++ (and also programming in general) and I was wondering how I can properly link the implementation, header, and main files. The purpose is to be able to run them as just those 3 files, instead of being dependent on running them through a .project file.

This is an example of the project I'm doing:

Cat.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "Cat.h"

using namespace std;

void speak()
{
    cout << "meow" << endl;
}

void jump()
{
    cout << "meow?" << endl;
}


Cat.h

1
2
3
4
5
6
7
#ifndef CAT_H
#define CAT_H

void speak();
void jump();

#endif // CAT_H 


CatMain.cpp

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Cat.h"

using namespace std;

int main()
{
    speak();
    jump();
    return 0;
}


The issue here is that when I open these files individually (without opening the .project file), I get the "undefined reference to WinMain@16" on Cat.cpp and "undefined reference" errors to the speak() and jump() functions on CatMain.cpp. The reason this might be an issue is because chances are that my professor won't have Code::Blocks to run it the same way as I do, so he will have to be able to run these standalone files rather than run the .project file.

Is there something I can do to link them together so that they're not dependent on the .project file to run properly?

Any help would be greatly appreciated =)
When you run this within Code::Blocks, it is in fact creating a complete, standalone executable file and then running it. That executable file is what you need. If you find wherever Code::Blocks put it, you should be able to simply run it.
But unless I open these files through the .project file, and open only these three files, it'll get those errors and won't create the exe file in the first place. That's the issue I'm having, which is that if I open only those three files, the program won't even build let alone run.
Last edited on
But unless I open these files through the .project file, and open only these three files, it'll get those errors and won't create the exe file in the first place.


How many times do you need to create the executable file? Just once. Create it once. Run it yourself directly to check it works. Copy it. Give it to someone else. They can run it directly. If your professor wants a working executable file, give it to him.

It strikes me that you don't understand how individual text files are turned into a working executable or library. This is something you should definitely take the time to understand, not least so that you grasp what I'm telling you. Here's a pretty good start: https://www.daniweb.com/programming/software-development/tutorials/466177/understanding-c-from-source-to-binaries
Completely missing the point. The reason I asked what I asked is because the goal is to make it so that others can run the main file (not the executable) without needing Code::Blocks.
What you're saying makes no sense. You cannot "run" the main file. The main file is a plain text file. You cannot run it. You cannot run it.

You can use it to build an executable. Once that executable is built, you can run the executable. You cannot "run" that text file you have named catmain.cpp

This is how executables are distributed; you write a text file (like your catmain.cpp file), and then you turn that into an executable file, and you give that executable file to people, and they can run it. They cannot run a plain text file named catmain.cpp

They could take that plain text file "catmain.cpp" and turn it into an executable file themselves, and then run that executable file. Is that what you're asking? Are you asking how you can give the plain text file "catmain.cpp" and the other text files to someone else and then they can create an executable file from it?

Every time you think you're "running" this file Catmain.cpp, you are not. You are instructing Code::Blocks to create an executable file, and to then run that executable file. That executable file is what gets run.
Last edited on
run the main file

Generally, C++ isn't interpreted. Or are you asking how to make it so that others can compile your program without needing Code::Blocks? If so, maybe look into some sort of build system, like CMake.

-Albatross
Hm sorry let me clarify.

Method 1: I open the project (which is these files) through Cat.project file in Code::Blocks. Here the files compile and the executable is created and runs normally.

Method 2: I open the project by opening only those three specific files. Here I get the compilation errors I mentioned.

So the question at hand is that what changes can I make to those three files so that method 2 works like method 1 would?

Are you asking how you can give the plain text file "main.cpp" to someone else and then they can create an executable file from it?


Yeah basically this. Up until now I haven't had to work with header files (a requirement for this project) so there were no such issues as I have described.

My fault for not articulating my question as clearly as I could have.
Last edited on
So the question at hand is that what changes can I make to those three files so that method 2 works like method 1 would?


This is a toolchain issue. There is nothing you can do to those 3 files which will help. Your professor should specify how you are to submit the work, so this seems like an unnecessary worry to me. Certainly, your professor should be familiar with whatever toolchain he is using.
The three files don't need changing at all. They're fine. If you wanted to then create an executable file from those three files, you need to do the following:

1) Compile the two *.cpp files into object files.
2) Link those object files into a single, new, executable file.

The link I pointed you at explains all this in some detail.

When you open your "project", Code::Blocks has the extra information that these files are all to be compiled together into a single executable. When you open the plain text files, Code::Blocks has not been told this, hence doesn't compile it in the way you hoped.

The reason we're having such trouble helping you is that you simply don't have the knowledge of how individual text files are turned into an executable, so when we ask you questions, it seems like we're talking about something else entirely.

As it is, if you want to drop having Code::Blocks do the compiling and linking for you, and do it yourself, you're going to have to identify what compiler you're using. If I had to guess, I'd guess that you're using a copy of Code::Blocks that came with the MinGW compiler included. However, that doesn't actually help your professor; you knowing how to manually create an executable on your machine with your tools might be useless to him, if he has different tools.

All this said, I think you might be overthinking it. If I were your professor, and you gave me those three text files, I would have no trouble in compiling and linking them myself in a matter of seconds using standard tools. In fact, I just did so. Took less than a minute.

The command I used to turn the three files into an executable (using the standard GCC compiler suite, which in this case handles compilation and linking for me) was:
g++ CatMain.cpp Cat.cpp
and here's me running the programme:
$ ./a.out 
meow
meow?


Has your professor asked you to provide the executable, or could you just provide the text files of code?

Last edited on
The purpose is to be able to run them as just those 3 files, instead of being dependent on running them through a .project file.


Well you can use command line tools to execute compiler against the source files.

for example:

cl CatMain.cpp -link /OUT cat.exe

no need to use any project files.
Last edited on
Note that codekiddy here has used a different compiler to the one I used:

cl CatMain.cpp -link /OUT cat.exe

This is using Microsoft's standard compiler, which is run using a program named "cl". You'll see that the syntax is different to the command I used, even though it does the same job on the same source code files.

When you have Code::Blocks build your project, this is all it's doing. It is literally entering this command, or one like it, into a command line and watching. If an executable is successfully built, it runs it for you. That's all it's doing. Code::Blocks is not "running" your plain text file named CatMain.cpp

You learning how to manually build a program on your machine, using whatever tools you happen to have, isn't going to be of any use to your professor - he could have completely different tools, which he probably already knows how to use.
Last edited on
Hmm I see, so I'm worrying over basically nothing or at least it's not a "problem" per se. Looks like in that case I'll just consult with my professor tomorrow. Thanks for the heads up guys!

Has your professor asked you to provide the executable, or could you just provide the text files of code?


My professor didn't go into detail about this stuff at all, so it seems like I was just overthinking and over-preparing things.
Topic archived. No new replies allowed.