如何使用Visual Studio 2010中不同C ++项目的函数? [英] How to use functions from different C++ projects in Visual Studio 2010?

查看:247
本文介绍了如何使用Visual Studio 2010中不同C ++项目的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Visual Studio 2010的同一个解决方案中构建两个C ++项目,可以相互交互。我在目录 C:\Users\me\Desktop\SolutionDir 下创建了一个解决方案。这两个项目分别在 C:\Users\me\Desktop\SolutionDir\FirstProject C:\Users \me\Desktop\SolutionDir\SecondProject

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.

我的第一个项目包含两个文件,一个头 function.h 和一个cpp文件 function.cpp

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

#pragma once
void print_stuff();



function.cpp



function.cpp

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

void print_stuff() {
    std::cout << "hello world" << std::endl;
}  


$ b $ p

我的第二个项目包含主文件 main.cpp

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

int main(void) {
    print_stuff();

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

我添加了目录 C:\Users\\ \\ me \Desktop\SolutionDir\ 在我的第二个项目配置属性> C / C ++>一般>其他包含目录。我仍然得到经典的错误:错误LNK2019:未解决的外部符号时调用 print_stuff()

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().

任何想法?

推荐答案

尝试将第一个项目构建为静态库项目属性/配置属性/常规/配置类型。

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. 在链接器/常规中,您可能需要添加到其他库目录第一个项目的 .lib .lib 的名称。 FirstProject.lib 或其任何名称的文件

  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.

这篇关于如何使用Visual Studio 2010中不同C ++项目的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆