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

3

10

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

My first project contains two files, a header function.h and a cpp file function.cpp

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 file main.cpp

main.cpp

#include "FirstProject\function.h"
#include <iostream>

int main(void) {
    print_stuff();

    int stop;
    std::cin >> stop;
    return 0;
}  

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

Any ideas ?

Pennebaker answered 3/5, 2012 at 20:21 Comment(0)
F
7

Try building the first project as a Static Library in Project Properties/Configuration Properties/General/Configuration Type.

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

  1. In Linker/General, you might need to add to "Additional Library Directories" the folder where the first project's .lib is built.
  2. In Linker/Input, you will need to add to Additional Dependencies the name of the .lib file like FirstProject.lib or whatever its name is.
Fescue answered 3/5, 2012 at 20:28 Comment(1)
Thanks a lot ! I totally forgot to specify that FirstProject should be a static library. It is now working !Pennebaker
U
3

Yes, you need to export the functions using _declspec(dllexport) and import them in the project that calls the functions with _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 first project, you tell the compiler the function is to be exported. However, when you include the file in a different project, that doesn't have FIRST_PROJECT_BUILD defined, you tell the compiler the function is implemented in a different library and should be imported.

Also, besides adding the extra include paths, you need to add the generated .lib files from the projects implementing the functions to the Extra dependencies tab in the Liner settings of your project configuration.

Unwatched answered 3/5, 2012 at 20:27 Comment(6)
This is assuming the first project is a .dllFescue
I gathered that the OP doesn't know about DLLs. You won't be able to tell them everything they need to know,but a link or something might get them on their way.Jennings
@ChrisA. why? Can't you dynamically load an exe? :)Unwatched
I was under the impression that _declspec(dllexport) only should be used when exporting from a .dll. Will it also work if the first project is a static library?Fescue
@ChrisA. IDK if it's needed for a static lib, but I don't see why it won't work.Unwatched
Yeah, you're right, it doesn't hurt in the case of a static library.Fescue
N
0

You can include a realPath for you include directory ! Like for your FirstProject include "./../" And the same include dir for your second Project like that you can move or copy your directory SolutionDir and it will always work !

For your unresolved linked you have to add the function.cpp and function.h in your First and Second Project You can place it in SolutionDir! Like that You always have two files for your first and second project instead of four !

Hope it helps !

Nervine answered 3/5, 2012 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.