简体   繁体   English

如何在Visual Studio 2010中使用来自不同C ++项目的函数?

[英]How to use functions from different C++ projects 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. 我想在Visual Studio 2010中的相同解决方案中构建两个可以相互交互的C ++项目。 I have created a solution under directory C:\\Users\\me\\Desktop\\SolutionDir . 我在目录C:\\Users\\me\\Desktop\\SolutionDir下创建了一个解决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 . 这两个项目分别在C:\\Users\\me\\Desktop\\SolutionDir\\FirstProjectC:\\Users\\me\\Desktop\\SolutionDir\\SecondProject

My first project contains two files, a header function.h and a cpp file function.cpp 我的第一个项目包含两个文件,一个头function.h和CPP文件function.cpp

function.h function.h

#pragma once
void print_stuff();

function.cpp 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主文件

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 . 我在我的SecondProject Configuration Properties > C/C++ > General > Additional Include Directories添加了目录C:\\Users\\me\\Desktop\\SolutionDir\\ I still get the classical error : error LNK2019: unresolved external symbol when calling the function print_stuff() . 我仍然得到经典错误: error LNK2019: unresolved external symbol调用函数print_stuff()error LNK2019: unresolved external symbol

Any ideas ? 有任何想法吗 ?

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. 在链接器/常规中,您可能需要将“附加库目录”添加到构建第一个项目的.lib的文件夹中。
  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. 在链接器/输入中,您需要向其他依赖项添加.lib文件的名称,如FirstProject.lib或其名称。

Yes, you need to export the functions using _declspec(dllexport) and import them in the project that calls the functions with _declspec(dllimport) . 是的,您需要使用_declspec(dllexport)导出函数,并在使用_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. 在第一个项目的配置中,将FIRST_PROJECT_BUILD添加到预处理器指令中。 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. 但是,当您将文件包含在未定义FIRST_PROJECT_BUILD的其他项目中时,您告诉编译器该函数是在不同的库中实现的,应该导入。

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. 此外,除了添加额外的包含路径之外,还需要将实现这些功能的项目中生成的.lib文件添加到项目配置的Liner设置中的Extra dependencies选项卡中。

You can include a realPath for you include directory ! 您可以为您包含一个包含目录的realPath! 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 ! 就像你的FirstProject包括“./../”和你的第二个项目一样包括dir,你可以移动或复制你的目录SolutionDir,它将一直有效!

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! 对于未解析的链接,您必须在第一个和第二个项目中添加function.cpp和function.h您可以将它放在SolutionDir中! Like that You always have two files for your first and second project instead of four ! 就像那样你的第一和第二个项目总共有两个文件而不是四个!

Hope it helps ! 希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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