简体   繁体   中英

How to run a Visual Studio solution without having to add the included files show in the Solution Explorer?

I have a solution called IncludeFileHelp which uses SampleClass.h and SampleClass2.cpp.

#include <SampleClass2.h>

void main() {
    SampleClass2 sampleClass2Obj;
    sampleClass2Obj.printHello();
}

SampleClass.h

#pragma
#include <iostream>

class SampleClass2 {
public:
    SampleClass2() {}
    void printHello();
};

SampleClass2.cpp

#include <SampleClass2.h>

void SampleClass2::printHello() {
    std::cout << "Hello from SampleClass2!" << std::endl;
}

I added to correct directory path of SampleClass2.h and SampleClass.cpp to the Additional Include Directories.

When I add SampleClass2.h to the Header Files in Solution Explorer and add SampleClass2.cpp to the Source Files in Solution Explorer, the program runs without any problems.

However, if I remove SampleClass2.h and SampleClass2.cpp from the Solution Explorer I get this error:

unresolved external symbol "public: void __thiscall SampleClass2::printHello(void)" (?printHello@SampleClass2@@QAEXXZ) referenced in function _main

The only way I know how to resolve this error is to drag and drop the SampleClass2.h and SampleClass2.cpp files back to the Solution Explorer. This becomes a tedious problem when copying or cloning the solution, as I have to manually add all the included files even though Visual Studio recognizes the class and it's functions.

What I can do so that my program can run in Visual Studio 2019 without having to have the included class header files and.cpp files in Solution Explorer?

The only thing I can think of is changing #pragma to:

#ifndef SAMPLECLASS2
#define SAMPLECLASS2

...

#endif /*SAMPLECLASS2*/

but that does not work.


Images of my Visual Studio Project

The most common method is to create a lib or dll. Then link the lib or dll to the project to meet your needs. You could refer the following Microsoft Documents for more information.

  1. Walkthrough: Create and use a static library

  2. Walkthrough: Create and use your own Dynamic Link Library (C++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM