0

I am new to c++, and I am trying to link two c++ files together, but received the error error: ld returned 1 exit status when trying to use the run build task on vs code. I am confused because neither of the files are allowing me to build. Here is my code in the files:

int add(int x, int y)
{
    return x + y;
}
#include <iostream>

int add(int x, int y); // needed so main.cpp knows that add() is a function defined elsewhere

int main()
{
    std::cout << "The sum of 3 and 4 is: " << add(3, 4) << '\n';
    return 0;
}

I've tried using cmake however couldn't get the set up to work, I've tried redoing the files from the beginning which gave the same error

  • 1
    please show a [mre], how are you building the code? What is the full compiler output and error message? – Alan Birtles Jan 12 '23 at 20:27
  • ***I've tried using cmake however couldn't get the set up to work*** Show the CMakeLists.txt that you wrote. Also did you install and enable the CMakeTools extension? Also did you look in the terminal and see which files were compiled? – drescherjm Jan 12 '23 at 20:29
  • ): in function `main': C:\temp\gcc\build-mingw-w64\mingw-w64-crt/../../src/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain' collect2.exe: error: ld returned 1 exit status Build finished with error(s). * The terminal process terminated with exit code: -1. * Terminal will be reused by tasks, press any key to close it. – Maxine Jones Jan 12 '23 at 20:32
  • The cmake text file is : cmake_minimum_required(VERSION 3.0.0) project(test.cpp VERSION 0.1.0) add_executable(test.cpp main.cpp) include(CTest) enable_testing() set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) include(CPack) – Maxine Jones Jan 12 '23 at 20:33
  • That means you built only the file containing `int add(int x, int y)` remember that by default VSCode only builds the active file and ignores all other source files. The VSCode documentation explains this and tells you how to fix. – drescherjm Jan 12 '23 at 20:33
  • ***The cmake text file is*** Edit the question. Click on the ***Edit*** link under the tags. – drescherjm Jan 12 '23 at 20:34
  • Got it, how do i build both files if possible? – Maxine Jones Jan 12 '23 at 20:35
  • The VSCode documentation tells you here: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Jan 12 '23 at 20:35

0 Answers0