0

I'm fairly new to c++ and programming in general and was watching the free tutorial on the freecodecamp.org youtube channel and when I got up to the point where I used multiple c++ files, I got multiple compiler errors with g++ and clang.

Here is main.cpp

#include <iostream>
#include "compare.h" 



int main(){

    int maximum = max(134,156);
    std::cout << "max : " << maximum << std::endl;

    

    
    return 0;
}

This is compare.cpp:

int max( int a, int b){
    if(a>b)
        return a;
    else
        return b;
}

And the compare.h file

    int max( int a, int b);//Declaration

When I try and build with clang I get:

Undefined symbols for architecture arm64: "max(int, int)", referenced from: _main in main-e30ba6.o ld: symbol(s) not found for architecture arm64 clang-13: error: linker command failed with exit code 1 (use -v to see invocation)

When I build with g++ I get:

Undefined symbols for architecture arm64: "__Z3maxii", referenced from: _main in cc3V4eOt.o ld: symbol(s) not found for architecture arm64 collect2: error: ld returned 1 exit statu

I've searched all over youtube and stack overflow and the only solution I found was to link the files with

g++ main.cpp compare.cpp -o main

But this only worked once and never again. Any help would be greatly appreciated thanks!

Edit

After some more research the only thing that gave me a definitive answer was to build with clang, get the error, then run:

clang++ main.cpp compare.cpp -o main

But I have to do this every time I make changes to the code and that just seems tedious and there has to be a better way. Also if I were to have multiple .cpp files I would have to run those into the command as well.

Musblum
  • 3
  • 3
  • Visual Studio Code's default behaviour is to compile only one cpp file. My VSC-fu is too weak to help you solve this, but if you look on the side for *visual studio code compile more than one cpp file* and you should find some answers. – user4581301 May 27 '22 at 21:17
  • In order to build a C/C++ project consisting of multiple source files, you would typically configure Visual Studio to use some kind of "makefile". Look here: https://code.visualstudio.com/docs/cpp/cmake-linux – paulsm4 May 27 '22 at 22:34
  • Does this answer your question? [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) – starball Aug 30 '23 at 19:00

1 Answers1

0

You could build multiple cpp file in VScode by shortcut .

  1. Create a build task by following the documentation
  2. Update the tasks.json to support build multiple cpp file:
{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "C/C++: clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++17",
        "-g",
        "${workspaceFolder}/*.cpp",
        "-o",
        "${fileDirname}/main"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
  ]
}
  1. Build with command Tasks: Run build task or shortcut.

enter image description here

ramsay
  • 3,197
  • 2
  • 14
  • 13
  • This helped a lot thanks. The problem was that instead of "${workspaceFolder}/*.cpp" I just had "${file}". Do you know how I can make it set to "${workspaceFolder}/*.cpp" by default so I don't have to copy and paste the same tasks.json file into every new project? – Musblum May 28 '22 at 08:02
  • As far as I know, you could give the global/shared configuration a try: https://github.com/microsoft/vscode/issues/1435 – ramsay May 28 '22 at 10:56