Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling header files for C++ using VSCode Code Runner

Tags:

I am using VSCode and the code runner extension to try to run a simple c++ project with include files.

The project is made of a main.cpp:

#include <iostream>
#include <time.h>
#include "cval.hpp"

using namespace std;

int main(void)
{


    putHello();

    return 0;
}

A hello.hpp header file:

#ifndef CVAL_H
#define CVAL_H
void putHello(void);

#endif

And the hello.cpp:

#include <iostream>

using namespace std;

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

If I define putHello() in main.cpp, the code compiles. If I include #include cval.cpp the code compiles.

However, when I only include the cval.hpp header and use the Code runner extension, I get the following error:

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

I understand this is a problem that I am not compiling cval.cpp so the linker cannot find it and link it properly. However, is there a way to get VSCode to automatically compile files that were included in headers?

I don't want to have to specify it all the time or have to include the .cpp files, and it is able to include these files with out a problem in my C code projects.

like image 433
easox Avatar asked Oct 24 '19 22:10

easox


People also ask

How do I create a header file in Visual Studio code C?

Place your caret on the first line of any C# or Visual Basic file. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Add file header. To apply the file header to an entire project or solution, select Project or Solution under the Fix all occurrences in: option.

How can I run C program in VS Code?

After writing the code, right-click on the program, as shown below. Click on the Run Code option or press Ctrl + Alt + N from the button.


1 Answers

There are two ways to solve it.

In settings(cmd + ,), find Run Code Configuration.

There is a tab 'Executor map' and open 'edit in settings.json'.

Then you can see the settings for code-runner extension (executor map).

Change the cpp part into this

"cpp": "cd $dir && g++ -o fileNameWithoutExt *.cpp && ./$fileNameWithoutExt"

This code compiles every cpp file in the current directory, so the header will be included properly.

A problem of this solution is, if there are other cpp files with main(), then it will not be compiled properly.

So you'd better use one folder for one cpp program.

I made a second solution to avoid this problem, but it only works for mac (because I made a function on the bash profile, and I'm not familiar enough with powershell script syntax to make that function on windows)

If you use macOS, try this one.

On vscode, press (shift + cmd + p) and type

Shell Command: Install 'code' command in PATH

And then open terminal, and type

sudo code ~/.bash_profile

if you use zsh, then instead of that, type

sudo code ~/.zshrc

and copy paste this code

function cppcompile()
{
  filename=$1
  re="^\#include \""
  while read line
  do
    if [[ $line =~ $re ]]; then
      temp=${line:9}
      temp1=${temp#\"}
      temp2=${temp1%\.*\"}
      g++ -std=c++11 -c $temp2.cpp
    fi
  done < $filename.cpp
  g++ -std=c++11 -c $filename.cpp
  g++ -o $filename *.o
  ./$filename
  rm *.o
}

and then open the executor map again, and change the cpp part into

"cpp": "cd $dir && cppcompile $fileNameWithoutExt"

cppcompile function will read your code and find headers, and compile headers and main code.

So it can compile the only one main code and the headers linked to that main, and it doesn't matter if there are other cpp files with main in the same directory.

like image 76
SW Park Avatar answered Sep 18 '22 22:09

SW Park