1

I have the stdafx.h file and a source (test.cpp) file in the default directory (C:\Users\Roland\Desktop\projects\test\test) together with a header (header.h) and a source (source.cpp) file in an other directory (C:\Users\Roland\Desktop\projects\add) than the default one. The content of these files is the following:

stdafx.h: empty

test.cpp:

#include "stdafx.h"
#include <header.h>


int main()
{
    int a = 2;
    int b = 3;
    swap(a, b);

    return 0;
}

header.h:

#ifndef _HEADER_H_
#define _HEADER_H_

void swap(int &a, int &b);

#endif

source.cpp:

void swap(int &a, int &b)
{
    int c;
    c = a;
    a = b;
    b = c;
}

I added the following path to the "Additional Include Directory" record: ../../add. When trying to build the solution, I received an error:

error LNK2001: unresolved external symbol "void __cdecl swap(int &,int &)" (?swap@@YAXAAH0@Z)

Could you please help me what would be the problem?

update:

I need a solution to making all the source files of the add directory available for my project.

Roli
  • 21
  • 5
  • The additional include directories serve for finding other *header* files. You need to add the *source* file to your project *explicitly*. – Aconcagua Aug 24 '18 at 04:05
  • Is there no any other way to use cpp files from other directories? – Roli Aug 24 '18 at 04:09
  • You need to make aware visual studio which files to compile. You can organise them differently than are on file system (MSVC uses filters for - not sure if calling them alike in the application itself, at least in the project files directly they are referenced as). – Aconcagua Aug 24 '18 at 04:27
  • Could you please tell me what are the **steps** of linking the `add` directory to my project? – Roli Aug 24 '18 at 04:54
  • I *think* you can just drag and drop the files from your directory to the folder/filter in MSVC. Cannot verify, though, no MSVC available at the moment... – Aconcagua Aug 24 '18 at 04:59
  • By the way: If you create new files in MSVC's default source directory externally (not using MSVC!), as far as I recall, they aren't added to the project automatically either... – Aconcagua Aug 24 '18 at 05:01

1 Answers1

0

If you have added source.cpp to your project, add source.h and put the swap function prototype in there. In your main function, include source.h and you should be fine.

Tbone281
  • 91
  • 1
  • 9
  • Oh, thanks, this is indeed a solution to the particular problem. However, what is the situation if there are a lot of such files to be added to several projects? The additions should be performed _one by one_, right? How to use _all_ cpp files of a directory another than the default without adding them to the project? – Roli Aug 24 '18 at 04:15
  • It should suffice just to add the header files and source files to the project. – Tbone281 Aug 24 '18 at 04:27
  • @Roli If you have a lot such files to be added to different projects, then I'd rather consider collecting these in a library and then link the other projects against this library. You can provide property sheets to share common settings with the library (e. g. include paths, defines, ...), so that you don't have to redo all this work with every other project. Problem only arises if you want to compile the common files with *different* compiler flags. *Maybe* you still can use property sheets to include source files, but you have to find out yourself, never tried that before... – Aconcagua Aug 24 '18 at 04:32
  • There is an answer here (https://stackoverflow.com/questions/10128337/visual-studio-link-error-when-including-headers-from-another-directory?rq=1) from dpiskyulev, where 'creation of a library project' is suggested. Could it help me? If so, how to perform it? – Roli Aug 24 '18 at 04:47
  • Could you please tell me what are the **steps** of linking the `add` directory to my project? – Roli Aug 24 '18 at 04:49