0

I'm learning C++ from scratch, and currently I'm practicing how functions can be separated to their own files using separate header files and cpp-files. I'm trying this out with a really simple example where I'm trying to print "Hello" with a function called printHello() declared in a separate file. This function is included to main code with header file printHello.hpp.

However, visual studio does not build my code and throws an error:

Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Build finished with error(s). The terminal process terminated with exit code: -1.

Here's my main file, function cpp-file and header file:

Main:

#include <iostream>
#include "printHello.hpp"
using namespace std;

int main(){
    cout << "Testing the header files." <<endl;
    printHello();
    return 0;
}

printHello.hpp:

#ifndef PRINTHELLO_HPP
#define PRINTHELLO_HPP
void printHello(void);
#endif

printHello.cpp:

#include<iostream>
#include"printHello.hpp"

using namespace std;

void printHello(void){
cout << "Hello"<<endl;
}

Interestingly, if I change #Include "printHello.hpp" to #Include "printHello.cpp" everything works without any problems.

I'm wondering if this error could be due to wrong arguments in tasks.json file. Currently, I have the following args defined:

    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++17",
                "-stdlib=libc++",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
    ]
  }

Has anyone had the same problem?

  • You should (almost) never include `.cpp` files. You need to specify both `.cpp` files when building, e.g. `clang++ 1.cpp 2.cpp`. – HolyBlackCat Sep 12 '21 at 14:40
  • One of your bugs is here: `"${file}",` the documentation tells you how to modify your tasks.json to support building more than 1 file: [https://code.visualstudio.com/docs/cpp/config-clang-mac#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-clang-mac#_modifying-tasksjson) – drescherjm Sep 12 '21 at 14:43
  • (1) *VScode for Mac with clang not recognizing C++ header files* is not what really happens; read the duplicate question and *all* answers very carefully. (2) Your tasks.json is only good for a project that has a single source file. If you have more than that, you probably want to learn about `cmake` and the corresponding vscode extension. – n. m. could be an AI Sep 12 '21 at 15:34

0 Answers0