0

I am having trouble linking my libraries to the main file in vscode. I've made a simple example:

main.cpp

#include "io.h"

int main()
{
    int x{read_number()};
    int y{read_number()};

    write_number(x + y);
    return 0;
}

io.h

#ifndef IO_H
#define IO_H

int read_number();
void write_number(int number);

#endif

io.cpp

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

int read_number()
{
    int x{};
    std::cout << "Enter an integer: ";
    std::cin >> x;
    return x;
}

void write_number(int number)
{
    std::cout << "The answer is " << number << '\n';
}

Attempting to build this with g++ and these settings

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-I",
                "${workspaceFolder}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}

results in

> Executing task: /usr/bin/g++ -g /home/morten/learn_cpp/chp2/main.cpp -I /home/morten/learn_cpp/chp2 -o /home/morten/learn_cpp/chp2/main <

/tmp/cca6uKjD.o: In function 'main':
/home/morten/learn_cpp/chp2/main.cpp:5: undefined reference to 'read_number()'
/home/morten/learn_cpp/chp2/main.cpp:6: undefined reference to 'read_number()'
/home/morten/learn_cpp/chp2/main.cpp:8: undefined reference to 'write_number(int)'
collect2: error: ld returned 1 exit status
The terminal process terminated with exit code: 1

I am used to using cmake instead so I am not quite sure how to do this with vscode. I have looked online and been recommended to pass the library path using the -I or -L flags to g++ but it doesn't seem to help anything.

Morten Nissov
  • 392
  • 3
  • 13
  • I think you are not linking libraries. Your problem is here: `"${file}",` The documentation explains how to use more than 1 source file with Visual Studio Code here: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Jul 10 '20 at 14:36
  • With that said if you wanted to build a library from `io.h` and `io.cpp` you probably want to use `CMake` with your `tasks.json`. Here is a question about that: [https://stackoverflow.com/questions/49584507/how-to-run-cmake-in-visual-studio-code-using-tasks](https://stackoverflow.com/questions/49584507/how-to-run-cmake-in-visual-studio-code-using-tasks) – drescherjm Jul 10 '20 at 14:39
  • If you're used to using cmake then use cmake and call that from the tasks. VS code's tasks really aren't suitable for compiling more than 1 cpp file – Alan Birtles Jul 10 '20 at 14:39
  • 1
    Install the extensions `ms-vscode.cmake-tools` and `twxs.cmake` and put a CMakeLists.txt in the root of your project. Now you can build, debug and run targets and tests from the bottom menu bar and you have syntax highlighting for CMake files. – Thomas Sablik Jul 10 '20 at 14:54
  • Problem is I'm trying to use vscode to avoid having to write cmake config files, but maybe this isn't really an appropriate solution? – Morten Nissov Jul 11 '20 at 14:12
  • @drescherjm This works for lib files in the same directory, but I am getting "io.h not found" errors when using an `/inc` folder – Morten Nissov Jul 11 '20 at 14:13
  • ***but maybe this isn't really an appropriate solution?*** I would say it's more difficult and more work to do that instead of just using CMake. – drescherjm Jul 11 '20 at 14:21
  • ***but I am getting "io.h not found" errors when using an /inc*** Then you need to add `inc` to your Include path. – drescherjm Jul 11 '20 at 14:21

0 Answers0