How to use functions from different projects in C ++ in Visual Studio 2010?

I would like to build two C ++ projects in the same solution in Visual Studio 2010 that can interact with each other. I created a solution under the directory C:\Users\me\Desktop\SolutionDir . These two projects were created respectively in C:\Users\me\Desktop\SolutionDir\FirstProject and C:\Users\me\Desktop\SolutionDir\SecondProject .

My first project contains two files: the function.h header and the cpp function.cpp file

function.h

 #pragma once void print_stuff(); 

function.cpp

 #include "function.h" #include <iostream> void print_stuff() { std::cout << "hello world" << std::endl; } 

My second project contains the main main.cpp file

main.cpp

 #include "FirstProject\function.h" #include <iostream> int main(void) { print_stuff(); int stop; std::cin >> stop; return 0; } 

I added the C:\Users\me\Desktop\SolutionDir\ directory to my second Configuration Properties > C/C++ > General > Additional Include Directories project. I still get the classic error: error LNK2019: unresolved external symbol when calling print_stuff() .

Any ideas?

+8
c ++ unresolved-external include visual-studio-2010 multiple-projects
source share
3 answers

Try to create the first project as a static library in the project properties / Configuration properties / General / Type of configuration.

Then in your project properties for the second project, you will need to change two things:

  • In Linker / General, you may need to add to the "Additional Library Directories" the folder in which the first .lib project is .lib .
  • In Linker / Input, you need to add the name of a .lib file, for example, FirstProject.lib , or as its name to additional dependencies.
+7
source share

Yes, you need to export functions using _declspec(dllexport) and import them into a project that calls functions using _declspec(dllimport) .

This duality is usually achieved with a macro:

 #pragma once #ifdef FIRST_PROJECT_BUILD #define IMPEXP _declspec(dllexport) #else #define IMPEXP _declspec(dllimport) #endif IMPEXP void print_stuff(); 

In the configuration of your first project, you add FIRST_PROJECT_BUILD to your preprocessor directives. That way, when you compile the first project, you tell the compiler that the function should be exported. However, when you include a file in another project that does not have FIRST_PROJECT_BUILD , you tell the compiler that the function is implemented in another library and should be imported.

In addition to adding additional inclusions, you need to add the generated .lib files from projects that implement functions to the Extra dependencies tab in the Liner settings of your project configuration.

+3
source share

You can enable realPath for you, including the catalog! As for your FirstProject, include "./../" And the same includes dir for your second project, as that you can move or copy the SolutionDir directory and it will always work!

For your unauthorized connection, you need to add the .cpp and function.h functions to your first and second projects. You can place it in SolutionDir! For example, you always have two files for your first and second projects instead of four!

Hope this helps!

0
source share

All Articles