1

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.

2
  • 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. Commented May 27, 2022 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: code.visualstudio.com/docs/cpp/cmake-linux Commented May 27, 2022 at 22:34

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
As far as I know, you could give the global/shared configuration a try: github.com/microsoft/vscode/issues/1435

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.