0

我将尝试尽快解释我想要做什么:

一个使用静态库的项目,该项目具有另一个作为依赖项。它生成一个名为 MyProject 的项目,链接在 MyLib1 上,链接在 MyLib2 上。下面是编译顺序:

  • 我的Lib2
  • MyLib1(链接到 MyLib2)
  • MyProject(链接到 MyLib1)

我正在使用 Visual Studio 2008,但在定义包含时遇到了一些麻烦。链接时,我使用属性“附加包含目录”(在项目属性 C/C++ 节点上)。这似乎在 MyProject 和 MyLib1 之间有效,但在 MyLib1 和 MyLib2 之间无效。例如:我在 MyLib2 中有一个名为 foo.cpp 的文件;使用 #include "foo.cpp" 使视觉工作室告诉 foo.cpp 是未知的(缺少文件或文件夹)。

为了确保它不是我给出的错误路径,我做了很多尝试,如下所示:将命令行中显示的路径(用于编译库)复制粘贴到 win explorer:我很好地看到了我的第二个库的源代码。我已经多次重制该项目,每次我使用不同的名称(迫使我注意这一点),一切似乎都定义明确(但不是“包括”)。

我真正找到使它起作用的唯一方法:使用 #include "c:\\foo.cpp"作为包含...非常适合可移植性!

这是解决方案的 Zip,您可以自己测试并告诉我出了什么问题:MyProject.rar

感谢您花时间帮助我!露西伯拉德

4

2 回答 2

1

首先,永远不要包含 *.cpp 文件。

其次,使用外部函数的前向声明:

void appellib2(void);

void appellib1(void)
{
    appellib2();
}

Third, right-click each project in the Solution Explorer, and select "Project dependencies..." and set-up proper dependencies: MyProject -> MyLib1 -> MyLib2.

At last, in properties for MyProject, set up additional dependencies MyLib1.lib.

Now I can build and run your project without errors.

UPDATE

Never rename *.cpp to *.h just to solve linking problems. If you have a definition in your *.h file you will unable to include it twice or more.

Here is your project YourProject.rar.

于 2009-09-14T05:35:51.140 回答
0

I've changed .cpp to .h. I Know I've never to include a CPP file but it was for testing purpose.

I've added prototype of each function but it doesn't works better (It don't include the file mylib2.cpp, the prototype is better but not usefull in this test).

I've set up project dependencies. It still fail.

我已经为 MyProject 设置了额外的依赖项 MyLib1.lib(我认为这是我能够找到的下一个错误,产生链接错误)。

通过这些修改,我仍然得到包含错误 mylib2.h:没有这样的文件或目录。

这是新的存档:MyProject.rar

如果你想办法让它工作,你能给我一份工作解决方案的档案吗?

谢谢,露西伯拉德。

于 2009-09-14T10:42:17.957 回答