0

I am working on a project and need to split the code into different files. I looked up tutorials on youtube because I had no idea how to spit code into different files. So I just followed a youtube tutorial exactly word by word, but after everything, the tutorial video code printed out the result but mine did not even after doing the exact same thing. But it just prints out the following in the terminal.

PS C:\Users\david\OneDrive\Desktop\account> cd "c:\Users\david\OneDrive\Desktop\account\" ; if ($?) { g++ bank.cpp -o bank } ; if ($?) { .\bank }
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\david\AppData\Local\Temp\ccCetn85.o:bank.cpp:(.text+0x26): undefined reference to `Date::Date(int, int, int)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\david\AppData\Local\Temp\ccCetn85.o:bank.cpp:(.text+0x32): undefined reference to `Date::display()'
collect2.exe: error: ld returned 1 exit status

This is the main file

// MAIN FILE
#include<iostream>
#include<string>
#include "account.h"

using namespace std;

int main(){
    Date val(2,12,2015);
    val.display();
}

This is the account.cpp file

// account.cpp FILE
#include "account.h"

Date::Date(int month, int day, int year){
    this->day = day;
    this->month = month;
    this->year = year;
 }

 void Date::display(){
     cout << month << "/" << day << "/" << year << "/" << endl; 
 }

This is account.h file

// account.h FILE
#pragma once
#include <iostream>

using namespace std;

class Date{
    private:
    int day,month,year;
    public:
    Date(int month = 1, int day = 1, int year = 2022);
    void display();
};

I am really lost on this I can't find any solutions. Any help is really appreciated. Thank you in advanced.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 2
    Looks like account didn't compile. Build command backs it up. Use `g++ bank.cpp account.cpp -o bank` – user4581301 Apr 28 '22 at 23:50
  • 1
    Nowhere in that command you posted does it mention `account.cpp` being compiled or part of the build process. – PaulMcKenzie Apr 28 '22 at 23:50
  • 2
    If you're using Visual Studio Code, this is a common problem: It defaults to building only one file. You have to hack a config file to get it to be smart enough to compile more than one file. I'm not VS Code savvy, but there'll be an answer covering this somewhere on site. – user4581301 Apr 28 '22 at 23:53
  • @user4581301 Ya I am using vs code. I have no idea how to fix it tho. – Devang Patel Apr 28 '22 at 23:54
  • 2
    *So I just followed a youtube tutorial exactly word by word* -- If the video didn't mention you need to compile all of your source files, not just one, and link all your source files, not just one, then that video is not worth it. As a matter of fact, learning C++ by video is not a good idea to begin with. – PaulMcKenzie Apr 28 '22 at 23:54
  • @PaulMcKenzie I did compile the account.cpp file. It is compiled without errors. – Devang Patel Apr 28 '22 at 23:54
  • 1
    *I did compile the account.cpp file* -- The command line you posted doesn't agree with you. But let's say you did compile it -- still, nothing on that command-line you posted says to use the object code from the compilation of `account.cpp`. – PaulMcKenzie Apr 28 '22 at 23:56
  • @PaulMcKenzie this is what i get when i compile the account.cpp C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main': C:/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain' collect2.exe: error: ld returned 1 exit status – Devang Patel Apr 28 '22 at 23:57
  • 1
    if [VS Code will not build c++ programs with multiple .ccp source files](https://stackoverflow.com/questions/47665886/vs-code-will-not-build-c-programs-with-multiple-ccp-source-files) doesn't help you out you'll have to add the VS Code configuration files to the question and wait for someone more familiar with the tool than Paul and I to come along. – user4581301 Apr 28 '22 at 23:57
  • 1
    Paul is correct. The current build command is `g++ bank.cpp -o bank`. It excludes account.cpp or, if you compiled account.cpp separately, account.o or account.obj, the most likely two outputs of compiling account.cpp. – user4581301 Apr 28 '22 at 23:59
  • @DevangPatel `ld.exe` is the linker, not the compiler. If you were to forget about VS code, and instead did all of this manually by dropping to the command-line, you will see that the build will be successful (or should be). So then the conclusion would be that the visual tool you are using to build using `g++` needs to know how to compile a multimodule project. It could be VS code, CodeBlocks, or whatever, you need to read the docs on the tool(s) you are using. – PaulMcKenzie Apr 29 '22 at 00:01
  • This is what i am getting after compiling account.h Program 'account.exe' failed to run: The specified executable is not a valid application for this OS platform.At line:1 char:98 + ... ount\" ; if ($?) { g++ account.h -o account } ; if ($?) { .\account } + ~~~~~~~~~. At line:1 char:98 + ... ount\" ; if ($?) { g++ account.h -o account } ; if ($?) { .\account } +~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException + FullyQualifiedErrorId : NativeCommandFailed – Devang Patel Apr 29 '22 at 00:02
  • Some helpful reading: [How does the compilation/linking process work?](https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work) – user4581301 Apr 29 '22 at 00:04
  • @PaulMcKenzie I'm sorry if I sound dumb but this is my first semester with C++ and I have no idea how to read errors and how to fix them. I have no idea what you mean by "g++ needs to know how to compile a multimodule project." – Devang Patel Apr 29 '22 at 00:07
  • Account.cpp and Bank.cpp are what are called translation units. They are separate modules that, once compiled, need to be assembled into a single program. Neither can work alone. You can compile them to object files separately, but after compiling, you need to link them together. This is a job generally best left to a [build automation tool](https://en.wikipedia.org/wiki/Build_automation), and I understand, but never exercised that understanding, that VSCode works well with CMake. If you call g++ with all of the files you need compiled all at once, it will compile and link them for you. – user4581301 Apr 29 '22 at 00:12
  • @user4581301 I'm sorry man but first of all what do need to do to process what I just read. I'm so lost but I feel so bad. – Devang Patel Apr 29 '22 at 00:16
  • [Focus on the link I posted earlier](https://stackoverflow.com/questions/47665886/vs-code-will-not-build-c-programs-with-multiple-ccp-source-files) and see if you can apply it. If you cannot, add the configuration files you modified to the question and the output of building with those new configuration files. That should make it precise enough that the current duplicate no longer applies and we can reopen the question for someone who knows how to beat Visual Studio Code into shape. – user4581301 Apr 29 '22 at 00:21
  • Alternately, consider using [Visual Studio Community edition](https://visualstudio.microsoft.com/vs/community/) or [Code::Blocks](https://www.codeblocks.org/). Both almost always work correctly right out of the box. Once you are more familiar with C++, how it works, and how to use the tools you can go back to VS Code. – user4581301 Apr 29 '22 at 00:21
  • @user4581301 I read the book but not all the way only the things that were assigned by the instructor to read. but there wasn't really anything about code in separate files. or compiler error problems. This is my first C++ class in school so I am a real noob at coding and especially error messages. and this is a program that I have to build for extra credit ( That I really need ) so my instructor didn't really go over it. – Devang Patel Apr 29 '22 at 00:25
  • Its in the VSCode documentation how to fix this. Also I have a recent answer that explains the problem and provides the solution: [https://stackoverflow.com/questions/70856563/vscode-g-it-isnt-finding-cpp-definition-files/70856902#70856902](https://stackoverflow.com/questions/70856563/vscode-g-it-isnt-finding-cpp-definition-files/70856902#70856902) Note: This solution assumes you are not using code-runner or some other build related extension. – drescherjm Apr 29 '22 at 00:29
  • 1
    Don't expect an instructor to require you to read everything, but do expect them to expect you read it anyway. When you reach something like this in an assignment, start looking around in the text for material on the topic you are struggling with. Odds are it'll be covered and probably covered in better detail than we can here on Stack Overflow. At the moment you aren't really struggling with C++ as much as you're struggling with a tool that's supposed to make your life easier. @drescherjm 's linked answer looks to be a cleaner version of what I posted earlier. – user4581301 Apr 29 '22 at 00:33
  • @user4581301 Ya your right. I will try to find the solution in the book and see if I can figure something out. – Devang Patel Apr 29 '22 at 00:43
  • @DevangPatel -- As others mentioned, this is not really a C++ issue, it's an issue with knowing how to use the tools to develop a program correctly. Note that C++ language itself only deals with individual modules and the source code inside of them. That's where the tools (especially the linker) come into play. Yes you compiled all of those C++ files separately and were successful -- the next step is to figure out how to use your build tools to bring those successfully compiled object modules under one umbrella. That's where you need to read what the instructions are for your IDE. – PaulMcKenzie Apr 29 '22 at 16:33
  • 1
    For most build tools, a fancy "tree" graph of all of your C++ modules show up, where the tool (VS Code, CodeBlocks, etc.) state that "I will compile this code, and link everything together for you". It's that step that is different among the differing brands of build tools out there. So that's the reason you need to read up on how to use your build environment correctly, because it differs. Some are easy to use (VS C++ Community Edition), some are harder to use or take more setup (VS Code). – PaulMcKenzie Apr 29 '22 at 16:36

0 Answers0