How to setup sublime 3 build for C++ with multiple files - c++

First of all, I'm a total noob on SO, so please go easy on me :)
That being said, my question might be really easy to answer:
How would I use sublime text 3 for c++ with multiple files (header/implementation files)
I've been trying some build systems provided by other people,
this is the one I'm currently using and seems to work just fine for compiling single .cpp files but is telling me some errors which I don't understand
here is my build config using g++
{
"cmd": ["g++.exe", "-std=c++14", "-o", "$file_base_name", "$file", "&&", "start", "cmd", "/c", "$file_base_name & echo. & echo. & pause"],
"shell": true,
"selector": "source.c++"}
here is my main.cpp:
#include <iostream>
#include <string>
#include "num.h"
using namespace std;
int main(){
Num n(35);
cout << n.getNum() << endl;
return 0;}
here is my num.h file:
#ifndef NUM_H
#define NUM_H
class Num
{
private:
int num;
public:
Num(int n);
int getNum();
};
#endif
and here is my num.cpp:
#include "num.h"
#include <iostream>
#include <stdlib.h>
Num::Num(int x){
num = x;
}
int Num::getNum()
{
return num;
}
all files are in the same directory and whenever I'm using g++ on the command like like this: (g++ (tdm64-1) 5.1.0)
g++ main.cpp num.cpp
there is no problem and everything works just fine
but whenever I'm trying to build it with sublime text it throws me this error
C:\Users\GEBRUI~1\AppData\Local\Temp\ccqq94my.o:main.cpp:(.text+0x1a): undefined reference to `Num::Num(int)'
C:\Users\GEBRUI~1\AppData\Local\Temp\ccqq94my.o:main.cpp:(.text+0x26): undefined reference to `Num::getNum()'
collect2.exe: error: ld returned 1 exit status
[Finished in 0.8s]
I would really appreciate it if someone could tell me what I'm doing wrong here :)

Once you start getting into the realm of needing to compile multiple files, it's really best to start using make or CMake instead of trying to build the compile commands yourself on the command line or in a build system. This way you wouldn't have to edit your build system every time you added a new file to your project.
There is a Make build system that comes with Sublime, but you will need to generate the Makefile externally, either by hand or using tools like autoconf and automake. The CMakeBuilder package looks like it would be useful for working with CMake (I haven't used it myself), although you can of course use external tools as well.

Related

My Class.cpp is not being seen by the linker

I am trying to write a C++ class in a separate header and cpp file using VS Code as my IDE with the 'run' and 'C++' extensions.
main.cpp
#include "Fan.h"
int main() {
Fan fan1;
return 0;
}
Fan.h
#ifndef FAN_H
#define FAN_H
class Fan {
public:
Fan();
};
#endif
Fan.cpp
#include "Fan.h"
#include <iostream>
using namespace std;
Fan::Fan() {
cout << "Fan Class" << endl;
}
I really can't seem to find anything popping out as obviously wrong. I am wondering if it is a setup problem with VS Code.
If I change #include "Fan.h" in main.cpp to "Fan.cpp" it works so it makes me think that the code works but that the linker is not setup right.
Would appreciate any help!
EDIT: Ok so I've tried the code in a different IDE and it worked. It's something to do with VS Code. Here is the error:
[Running] cd "c:\Users\<USER>\Desktop\Fan\" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:\Users\<USER>\Desktop\Fan\"tempCodeRunnerFile
C:\Users\<USER>\AppData\Local\Temp\cclMFKxO.o:tempCodeRunnerFile.cpp:(.text+0x57): undefined reference to `Fan::Fan()'
collect2.exe: error: ld returned 1 exit status
It sounds like the IDE is only compiling main.cpp. You need to find the command that is compiling main.cpp and ensure that it also compiles fan.cpp into fan.obj. You will also need to ensure that both main.obj and fan.obj are passed to the linker (which produces the executable program, main.exe or whatever).
There are two steps involved here:
cpp -> obj (compiling each source file into a matching object file)
obj -> exe (linking many object files into an executable)
I would say to make a CMakeLists.txt file and add main.cpp and fan.cpp into the add_executable section. Then VS can handle and run files through CMake.

"undefined reference to `WinMain#16'" Error in gcc editor

I am just learning c++ and began to watch a youtube tutorial by thenewboston. Unfortunately he is using Code::Blocks while I am using gcc and I do not have the option to create new class files with a button click and so had to manually create them.
I dont understand why the same code in Code::Blocks and gcc will work in Code::Blocks but not gcc. Does gcc require different coding for the same language?
EDIT: I have downloaded and tested in Code::Blocks myself
Other questions talk of how I need to give windows an entry point, but I dont know how to do that.
Test.cpp Code:
#include <iostream>
#include "ClassTest.h"
using namespace std;
int main() {
ClassTest bo;
}
ClassTest.h Code:
#ifndef CLASSTEST_H
#define CLASSTEST_H
class ClassTest {
public:
ClassTest();
};
#endif // CLASSTEST_H
ClassTest.cpp Code:
#include <iostream>
#include "ClassTest.h"
using namespace std;
ClassTest::ClassTest() {
cout << "blah blah" << endl;
}
I'm not quite sure I understand what the question is; I'm going to take it as "how do I get these three files to build into a .exe that I can run from the Windows commmand line?"
The answer is to run something like this on the command line, in the folder with the files:
g++ -c Test.cpp -o Test.o
g++ -c ClassTest.cpp -o ClassTest.o
g++ Test.o ClassTest.o -o Test.exe
The first two commands build each CPP file into an "object file", which isn't a whole program by itself but which contains the compiled version of the code in that CPP file. The last command tells the compiler to paste together the two object files into a program, and resolve all the cross-references between them. (For example, the part where Test.cpp constructs a ClassTest object needs to end up calling the ClassTest constructor code from ClassTest.cpp.)
Code::Blocks is an IDE and works out how to build each source file in your project and link them together by itself. But if you aren't using an IDE, you need to do that in another way. You can either do it manually like this, or you can write a Makefile that will check which code files have changed and rebuild and re-link everything that depends on them when you run the make command, which is how most people do it.
As for "giving Windows an entry point", that probably refers to GUI applications that want to display windows on the screen. For console programs like the one you have written, the "entry point" is main(), and you just print stuff to the command line window. To make actual Windows-style GUI windows of your own, you need to use the Windows API, which I can't tell you much about.

C++ run project as 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
#include <iostream>
#include "Cat.h"
using namespace std;
void speak()
{
cout << "meow" << endl;
}
void jump()
{
cout << "meow?" << endl;
}
Cat.h
#ifndef CAT_H
#define CAT_H
void speak();
void jump();
#endif // CAT_H
CatMain.cpp
#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 =)
To run this or any C++ program without an IDE, you only need a C++ compiler available on the command line. (You may have to set an environment variable to the path to the compiler to use it on the command line)
To compile and run the code one could run the following commands on the command-line. (assuming a compiler such as GCC or MinGW, which uses the command g++ to compile C++)
g++ -c Cat.cpp (produces an .o object file for the implementation of Cat.cpp and Cat.h)
g++ CatMain.cpp Cat.o (compiles the main function as an executable and links the Cat.o to it)
a (in windows cmd)
./a.out (on *nix systems)

undefined reference to function code blocks

main.cpp
#include <iostream>
#include <string>
using namespace std;
void echo(string);
int main()
{
echo("hello");
cout << "Hello world!" << endl;
return 0;
}
print.cpp
#include <iostream>
#include <string>
void echo(string code){
cout << code;
}
After compiling the code in code blocks 12.11, it gives me that error:
undefined reference to `echo(std::string)
I use windows 7 x64.
I have added the directory; Project>build options > search directories and added the current working directory.
All the files are in one console project in code blocks
I believe you should read up a bit more on namespaces usage. You are missing std in print.cpp.
Generally, while starting to learn cpp or getting a grip of the language you should always try writing full names of the classes along with the namespaces. Eventually with practice and some oversights (like now) you will learn why you really need them. In a nutshell namespaces are great:
When you are writing code over multiple files
Compartmentalize your code into separate blocks.
Also, using namespace std; should be used within cpp files mostly (otherwise headers get mangled up.
Anyways, try changing your code to this:
#include <iostream>
#include <string>
void echo(std::string code){
std::cout << code;
}
Then your results will look like this:
> g++ main.cpp print.cpp -o a.out
> ./a.out
helloHello world!
You should get more than that linker error, since you use string without any namespace in your print.cpp file. And if that source file doesn't compile it can't be linked with, and you will get the linker error you have.
Change to e.g.
void echo(std::string code) { ... }
And you do try to link with the object file created from print.cpp ?
I know this is old, but for anyone looking to solve this issue, the following may be a solution for you. If you have g++ follow c++ 11 under project->build options (check your options anyway) then you must check that box for all files you make in the project for the error to be cleared up. I had that annoying undefined reference thing too but now it is gone!
Try "Project/Properties/Build Targets tab". There you should find "Build target files" field. In that filed find "print.cpp" and click the checkbox (now the compiler will build print.cpp).
Some usefull information on Project management in CB
http://www.codeblocks.org/docs/main_codeblocks_en.html
When dealing with strings in C++ its best to sue std::string and your code seems to be wrong with a changes like using std::cout instead of plain cout another thing you need to be careful is linking your files especially files in different directories you need to tell code blocks were to find this print.cpp by going to build option and go for the search tab directory and point to where print.cpp is other wise the other approach is to just build a project which will have the main.cpp and and then add print.cpp class to current project I hope this will be of some help

Beginner working with objects and classes getting the following errors

This is a tutorial I've been following and I've done everything that it tells but it doesn't work. I have three files: the main.cpp, burrito.h (the class), and burrito.cpp.
And here are the three files respectively.
main.cpp
#include <iostream>
#include "Burrito.h"
using namespace std;
int main() {
Burrito bo;
return 0;
}
Burrito.h
#ifndef BURRITO_H
#define BURRITO_H
class Burrito {
public:
Burrito();
};
#endif // BURRITO_H
Burrito.cpp
#include <iostream>
#include "Burrito.h"
using namespace std;
Burrito::Burrito() {
cout << "Hello World" << endl;
}
When I build and run I get the following error:
...undefined reference to `Burrito::Burrito()'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 6 seconds)
1 errors, 0 warnings
I'm compiling using CodeBlocks.
I'm using CodeBlocks
This is the issue.
If you’re starting to learn C++ it’s (unfortunately) essential to learn about translation units. An IDE like Code::Blocks hides this detail from you – and does it wrong, in this case (this isn’t really the fault of Code::Blocks though, it can’t automatically guess what to do in this case without configuation).
In the beginning, drop the IDE, go to the command line for compiling. Compile the two translation units separately and link them together explicitly.
g++ -o burrito.o burrito.cpp
g++ -o main.o main.cpp
g++ -o main main.o burrito.o
Every good beginners’ C++ book will explain how this works.
When you're linking objects together to get the final executable file you're forgetting to link the compiled-object from burrito.cpp file correctly
If you're building with a Makefile, your final output rule should have something like "-o main main.o burrito.o"
Using Code Blocks 13.12 I right clicked on the Burritto.cpp file chose Properties, then chose the Build tab and checked the compile file and link file check boxes then clicked ok saved everything then ran and it worked great.