0

So I am developing a project and I am almost at the end. I am working with 3 files: main.cpp, game.h, game.cpp . I used header files because it was told to me that it was good practice. However, I am having some issues and problems compiling and building the project in vscode. When including the corresponding .cpp file (#include "game.cpp") the ide recognises all functions, builds the project and runs it with no probs at all, but when I try including the header file (#include "game.h") the console displays a message saying that it hasn't found any function called in the main.cpp file. I have tried to look up this issue in various websites and forums, tried cmake, modifying c_cpp_properties.json and tasks.json files and I haven't solved this issue yet. Either I am doing something wrong (which is very likely given that I am still a beginner) or there's something wrong that I am not aware of.

I'd like to add that, for running and compiling "normal" .cpp files I am using the CodeRunner extension and to debug I use gcc/gdb.

Console output:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\zebar\AppData\Local\Temp\ccjgUQbH.o:main.cpp:(.text+0xe): undefined reference to `menu()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\zebar\AppData\Local\Temp\ccjgUQbH.o:main.cpp:(.text+0x30): undefined reference to `ReadRules()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\zebar\AppData\Local\Temp\ccjgUQbH.o:main.cpp:(.text+0x4a): undefined reference to `play()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\zebar\AppData\Local\Temp\ccjgUQbH.o:main.cpp:(.text+0x64): undefined reference to `leaderboard()'
collect2.exe: error: ld returned 1 exit status

My c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

My tasks.json :

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${workspacefolder}/game.cpp", "${workspacefolder}/game.h", "${workspacefolder}/main.cpp", 
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        },
       {
            "type": "shell",
            "label": "build",
            "command": "make",
            "args": [
            "CONF=Debug",
            "-C",
            "C:\\Users\\zebar\\.vscode"
            ]
       }
    ],
    "version": "2.0.0"
}

Main file (main.cpp):

#include "game.h"
#include <iostream>
#include <thread>
#include <chrono>

using namespace std;


int main(){
    int menu_choice;  // menu option

    while(true) {
        // Initiate Menu
        menu_choice = menu();

        switch (menu_choice) {
            case 1:     // Rules of the game
                ReadRules();
                cout << endl;
                break;

            default:  // Exit the game
                this_thread::sleep_for(chrono::seconds(2));
                return 0;
        }
    }
}

Header file (game.h):

#ifndef T02_G11_GAME_H
#define T02_G11_GAME_H

#include <iostream>
#include <fstream>
#include <string>
#include <thread>
#include <chrono>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;


/**
Main menu
 */
int menu();

/**
Rules of the game
 */
void ReadRules();

#endif //T02_G11_GAME_H

game.cpp:

#include "game.h"
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;


int menu(){
    int menu_choice;
    // Main Menu
    cout << "1) Rules" << endl;
    cout << "2) Play" << endl;
    cout << "3) Leaderboard" << endl;
    cout << "0) Exit" << endl;

    // Ask for menu_choice
    while (true)
    {
        this_thread::sleep_for(chrono::milliseconds(250));
        cout << "Option: "; cin >> menu_choice;
        if(cin.eof()) return 0;
        if(cin.fail());
        else if(menu_choice == 0) return 0;
        else if(menu_choice == 1 || menu_choice == 2 || menu_choice == 3) return menu_choice;
        cin.clear();
        cin.ignore(10000, '\n');
        cerr << "Input a valid operation! (0, 1 or 2 to proceed)" << endl;
    }
}


void ReadRules(){
    string line;
    ifstream myfile ("rules.TXT");  // file where are the rules
    int goBack;

    // Open file and print all the lines
    if(myfile.is_open())
    {
        while (getline(myfile, line)) cout << line << '\n';
        myfile.close();
    }

    //Go back to main menu by pressing '0'
    cout << "Press '0' to go to the main menu" << endl;
    cin >> goBack;

    //Test input while invalid
    while(true){
        cin >> goBack;
        if(cin.eof()) return;
        if(cin.fail());
        else if(goBack == 0) break;
        cin.clear();
        cin.ignore(10000, '\n');
    }
}

note: this is not the whole project, it is too big to post.

I'd be really thankful if someone could help me.

  • You should never include ```.cpp``` file, include ```.h``` file which should be your interface and use your function through that interface, but also in some ```.cpp``` file (in ```game.cpp``` in your example) you should also have definition for your functions in ```.h``` file. If it's still unclear, post your files. – Karen Melikyan Apr 26 '21 at 10:37
  • Everything you said is done correctly, a friend of mine can run everything flawlessly in CLion (I think it's because it compiles or links files automatically or much more easily than VS). The project is really big but I can give you an example so you understand it better. I'll post at the end of the first question. – Zé Araújo Apr 26 '21 at 10:46
  • I think you just don't have your all files in the project, because it should compile fine if you do it right, not such familiar with VS Code, but in VS you have project files in which you have specified all files of your project, should be something similar for VS too, try to check that. – Karen Melikyan Apr 26 '21 at 10:58
  • @KarenMelikyan i already tried creating an executable with all these 3 files and I don't know why it didn't work. Also created a MakeFile and refered these 3 files and didn't work. Modifying tasks.json and c_cpp_properties.json and had the same result. I dunno what else I can do ahah – Zé Araújo Apr 26 '21 at 11:11

0 Answers0