1

I am trying to reference two files which are located outside of my project's directory, a .h file and it's corresponding .cpp file. When I add the directory location to the  "Additional Include Directories"  section of the project it compiles and runs fine. However, when I use anything that is defined within the .cpp file, it gives me an error either saying that there's an "unresolved external symbol" or that there is a "undefined identifier".

The files are as follows:

// test.h
// external file
#pragma once
#ifndef TEST_H
#define TEST_H

int what();

#endif
// test.cpp
// external file
#include "test.h"

int what() {
    return 1;
}
// main.cpp
// project file
#include "test.h"
int main() {
    what(); // LNK2019 unresolved external symbol
    return 0;
}

Is there any way to link the .cpp file to the project?

I know that this can be "fixed" by converting the files to a .lib or .dll, but I wish to use .cpp files unless I absolutely cannot.

44a3
  • 35
  • 4
  • 1
    Terminology: You don't really want to link the cpp file to the program. You want to link the compiled output of the cpp file, the object file. – user4581301 Mar 21 '23 at 16:51
  • Please confirm that you are using Visual Studio and not Visual Studio Code. Visual Studio generally looks after the cpp files it knows about for you. Visual Studio Code requires modifications to some of the configuration files to instruct it to successfully build a program comprised of multiple cpp files. – user4581301 Mar 21 '23 at 16:53
  • @user4581301 I am using Visual Studio 2019, not Visual Studio Code. – 44a3 Mar 21 '23 at 17:06
  • Groovy. Thanks. Narrows down the field. If you created both cpp files in Visual Studio normally it will see, compile, and link the files for you. Are all of the cpp files spotted by the project and listed in the solution explorer (typically in the right-hand sidebar)? – user4581301 Mar 21 '23 at 17:11
  • @user4581301 The `.cpp` file is not present. However, the `.h` file *is* present in the "External Dependencies" section. – 44a3 Mar 21 '23 at 17:19
  • 2
    OK. Right-click the Source Files section and select Add. Then use the browser to hunt down the file and make Visual Studio aware of it. The IDE should do the rest from there. – user4581301 Mar 21 '23 at 17:30
  • 1
    The `.cpp` file needs to be present and part of your project for it to be compiled. Just including the header does not cause the compiler to search and find the cpp file(s) that implements the functions specified in the header and compile that c++ does not work in that manner. – drescherjm Mar 21 '23 at 17:34
  • @user4581301 This worked, thank you. Is there another way to accomplish this, or is this the only way? – 44a3 Mar 21 '23 at 18:03
  • 1
    You can use Visual Studio to create all of the files in the project, but if you use other tools or have copied the files from somewhere, you need to Add so that Visual Studio knows about them. – user4581301 Mar 21 '23 at 18:04
  • 1
    Including .cpp files has been tested to solve this problem, including .cpp files is discouraged for a number of reasons, but not strictly prohibited. I recommend you refer to user4581301's method or put the function implementation into the .h file. – Yujian Yao - MSFT Mar 22 '23 at 02:02

1 Answers1

-2

After testing, including .cpp files solves the problem. There are a number of reasons why including .cpp files is discouraged, but not strictly prohibited.

You could find the answer in this issue about why including .cpp files should be avoided.

I recommend you refer to user4581301's method or put the function implementation into the .h file.

Yujian Yao - MSFT
  • 945
  • 1
  • 3
  • 9