问题描述
简短的问题是:如何使用Visual Studio创建/编译/运行在不同目录中具有源代码的项目.
以下是详细信息:
我在一个目录中有一个类定义文件(.hpp)和实现文件(.cpp),在另一个目录中有一个main.cpp文件.在Visual Studio 2008中,我创建了一个新项目,并将main.cpp放入该项目中.我将类文件目录添加到其他包含目录(在项目上单击鼠标右键->项目属性->配置属性-C/C ++-常规->其他包含目录= C:\ Test \ cpp).
I have an class definition file (.hpp) and implementation file (.cpp) in one directory and I have a main.cpp file in another directory. In Visual Studio 2008, I created a new project and put main.cpp in that project. I added the class file directory to Additional Include Directories (right click on the project -> project properties -> Configuration Properties - C/C++ - General -> Additional Include Directories = C:\Test\cpp).
当我这样做时,在编辑main.cpp时intellisense可以正常工作.生成项目时,它可以正常编译,但是出现链接错误,例如:
When I do this, intellisense works fine when editing main.cpp. When I Build the project, it compiles fine, but I get link errors such as:
据我所知,Visual Studio实际上并没有编译Test.cpp(我看不到为其创建任何.obj文件).我尝试过在编译/构建main.cpp之前单独进行编译,但这没有任何区别.有没有解决的办法?当我在网上搜索答案时,我看到很多人忘记为链接程序添加库,但是我没有处理任何库.
As far as I can tell, Visual Studio isn't actually compiling Test.cpp (I don't see any .obj files being made for it). I have tried compiling it separating, before Compiling/Building main.cpp, but that didn't make any difference. Is there a way around this? When I search the web for answers, I see a lot of people forgetting to include libraries for the linker, but I'm not dealing with any libraries.
我还通过将Test.hpp和Test.cpp移到与main.cpp相同的目录中,验证了此代码可编译并正常运行-正常工作.我只是坚持使用该设置,但是我需要能够将不同目录的源用于我正在处理的项目.
I have also verified this code compiles and runs correctly by moving Test.hpp and Test.cpp to the same directory as main.cpp - that worked without any problems. I would just stick with that setup, but I need to be able to use source from different directories for a project I'm working on.
这是3个文件:
-
C:\ Test \ cpp \ Test.hpp
C:\Test\cpp\Test.hpp
#ifndef TEST_H
#define TEST_H
class Test
{
private:
int mynum;
public:
Test();
int add(int num);
};
#endif //TEST_H
C:\ Test \ cpp \ Test.cpp
C:\Test\cpp\Test.cpp
#include "Test.hpp"
Test::Test()
{
mynum = 0;
}
int Test::add(int num)
{
return mynum += num;
}
C:\ Visual Studio Projects \ MyProject \ main.cpp
C:\Visual Studio Projects\MyProject\main.cpp
#include <iostream>
#include <Test\Test\Test.hpp>
int main(int argc, char *argv[])
{
Test test;
std::cout << "Add 5 = " << test.add(5) << std::endl;
return 0;
}
推荐答案
最重要的是,您必须编译Test.cpp并以某种方式与其链接.两种选择:
The bottom line is that you have to compile Test.cpp and link with it somehow. Two options:
- 将Test.cpp添加到您的项目中
- 创建一个库项目,例如TestLib.将Test.cpp添加到该项目并进行编译.然后将您的主项目与TestLib链接.
在第二种情况下,您可以在VisualStudio中具有相关项目.这样,它将正确地提取适当的Debug/Release/etc.构建主项目时的库版本.它还将从正确的文件夹中获取它.例如.无需手动修改链接器设置.
In second case you can have dependent projects in VisualStudio. This way it will properly pull proper Debug/Release/etc. version of the lib when you build your main project. It will also get it from the right folder. E.g. no need to mess with linker settings manually.
这篇关于包括来自其他目录的标头时,Visual Studio链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!