0

My professor has pointed out that this isn't normal, but I have been struggling to fix the error.

I am using VSCode as my IDE, and have installed the C/C++ extensions. I haven't been able to find anyone else describing this error, and my professor has been unable to help me find the cause, and has told me to ask a coding community to look for help.

I fear that something may be corrupted with my linker, as that was a concern my professor had.

I am able to run my driver with all of these include statements:

#include "unsorted.h"
#include "DateType.h"
#include "PersonType.h"
#include "StudentType.h"
#include <iostream>
#include "DateType.cpp"
#include "PersonType.cpp"
#include "StudentType.cpp"
#include "unsorted.cpp"
using namespace std;

but if I comment out the .cpp files, the driver will fail to compile. My professor says I should only be including the .h files and not the .cpp files, but that causes errors.

I tried to comment out the .cpp files, but the code won't compile like that, even though it should only require the header file.

Why is this happening?

I changed my tasks.json file to include this

"type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${workspaceFolder}\\*.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"

it didn't solve my issue. I am running windows 10, and can't fix this compiler bug. I can't find the source of the problem, and the related link didn't help.

Mr Hupwop
  • 1
  • 1
  • Are you compiling the other cpp files – Cameron R Mar 07 '23 at 17:25
  • 2
    Don't ever `#include` .cpp files. Your build tools should compile each .cpp file separately and link the resulting object files together. – Pete Becker Mar 07 '23 at 17:31
  • @PeteBecker [Unity builds](https://en.wikipedia.org/wiki/Unity_build) are a perfectly reasonable thing. They [are used](https://devblogs.microsoft.com/cppblog/support-for-unity-jumbo-files-in-visual-studio-2017-15-8-experimental/) with Visual Studio by many. – David Schwartz Mar 07 '23 at 17:34
  • @DavidSchwartz -- and `#include ` is used by many, as well as `using namespace std;`. That doesn't make them a perfectly reasonable thing. Learning bad habits just leads to problems later in life. – Pete Becker Mar 07 '23 at 17:41
  • @PeteBecker It's an [officially supported Visual Studio](https://devblogs.microsoft.com/wp-content/uploads/sites/9/2019/02/Unity-Build-Post.png) feature. – David Schwartz Mar 07 '23 at 18:00
  • "*[my professor] has told me to ask a coding community to look for help*" - wow, that's a first! "*my professor has been unable to help me find the cause ... [he] says I should only be including the `.h` files and not the `.cpp` files*" - then he obviously knows the cause, but is unwilling to share the solution, or really doesn't know it himself. Either way, you should find a more competent professor. He is right about one thing, though - don't `#include` `.cpp` files. Your project should *compile* each `.cpp` file separately and then *link* the object files together, like Pete said. – Remy Lebeau Mar 07 '23 at 18:44
  • @DavidSchwartz -- well, there you go: Microsoft is always right! – Pete Becker Mar 07 '23 at 19:59
  • My professor seems to know nothing about the VSCode IDE. I've been trying to get a solution from him for weeks but can't. I am unable to find the error in my tools? I replaced the code that needed to be replaced in tasks.json, but it still isn't compiling without the .cpp files. – Mr Hupwop Mar 07 '23 at 20:04
  • @MrHupwop It should compile just fine. But you need to somehow link all the compiled code together. Are you compiling all the .cpp files? – David Schwartz Mar 07 '23 at 22:56
  • Make sure that you are not using code-runner and that the task is being run. Look at the terminal for the exact build command that is run. – drescherjm Mar 07 '23 at 23:32
  • @DavidSchwartz I can get it to compile but it requires me to write an include statement for every linked .cpp and .h file, which everyone is telling me not to do because it's improper. My professor is telling me to switch to Eclipse but I'm struggling to run code there too. – Mr Hupwop Mar 09 '23 at 16:55
  • @drescherjm I think this is my exact build command: cd "c:\Users\john3\C++ Workspace\PlusCode6\Chapter1\Fraction\" && g++ fracDr.cpp -o fracDr && "c:\Users\john3\C++ Workspace\PlusCode6\Chapter1\Fraction\"fracDr Am I using code runner? – Mr Hupwop Mar 09 '23 at 17:14
  • It does not appear to be using your tasks.json at all. That would happen if code-runner was active and possibly if you selected a build different task than the one you are showing us. – drescherjm Mar 09 '23 at 19:37
  • You may want to try the official VSCode tutorial which explains the 3 json files and shows you how to build a simple example. – drescherjm Mar 09 '23 at 19:40
  • @MrHupwop Your command is missing a `-c` to tell `g++` not to try to link the individual C++ file. – David Schwartz Mar 09 '23 at 22:25

0 Answers0