0

I am having an issue where I have a ton of custom C/C++ header files (and respective .cpp files) where I would like to include them in other scripts in the visual studio code environment. I'm using windows 10. I thought I understood C/C++, but apparently not. Anyway, my goal is to be able to run something like the code below:

#include <iostream>
#include <JeffsLaboratory_Earth.h> // This causes the error, even though VS code opens the correct file if I right-click on it

int main() {
    int x = 10;
    int y = 20;
    earth ear;

    float f = ear.atm_alt_temp_dens;
    std::cout << "Hello World" << std::endl;
}

where "JeffsLaboratory_Earth.h" points to a header file I have, which is in the same folder as the equivalent "JeffsLaboratory_Earth.cpp" file. This folder is local.

However, I am having tons of difficulty getting this to work. I am also including below the JSON files as well. As you can see, I have included the path to which this library/header file resides, but I am still getting "undefined reference to earth::earth()" and it terminates. Even though VS code will point to the right location of JeffsLaboratory_Earth.h when I right-clock on it. I know for a fact this code and library works, as I have used it in other applications, so that is not the issue. Any help would be much appreciated! Thanks.

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/JeffsLaboratory/commonsoftware/C++/PQRIntegratedSim_libraries"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "cStandard": "gnu11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "gcc-x64",
            "browse": {
                "path": [
                    "C:\\MinGW\\lib\\gcc\\mingw32\\9.2.0\\include\\c++"
                ]
            }
        }
    ],
    "version": 4

task.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g", "main.cpp",
                "-I", "C:\\JeffsLaboratory\\commonsoftware\\C++\\PQRIntegratedSim_libraries",
                "${file}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "preLaunchTask": "echo",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
  • When you have more than one source file you need to compile all the source files. Just including a header is not sufficient. – john Jan 08 '21 at 08:22
  • if that header is in your project folder (i.e. where is main.cpp) try to use `#include "JeffsLaboratory_Earth.h"`. `#include` means `JeffsLaboratory_Earth.h` is in special folder (in linux it is /usr/include/) – Kanony Jan 08 '21 at 08:32

1 Answers1

2

"undefined reference to earth::earth()"

is absolutely not the same as:

#include <JeffsLaboratory_Earth.h> // This causes the error, even though VS code opens the correct file if I right-click on it

The second error would indicate an incomplete include path. The first indicates a link error.

From what I see, you don't compile and link JeffsLaboratory_Earth.cpp. You must add it to your build configuration.

See What is an undefined reference/unresolved external symbol error and how do I fix it?

Bktero
  • 722
  • 5
  • 15