简体   繁体   English

将静态库链接到Visual C ++中的新项目

[英]Linking a static library to a new project in Visual C++

I am just beginning to learn C++ and using Visual Studio 2017 as the IDE. 我才刚刚开始学习C ++,并将Visual Studio 2017用作IDE。 I want to stay away from the CLR, and even Windows specific code as much as possible. 我想尽可能地远离CLR,甚至Windows特定的代码。 Every new project option seems to say 'Windows', and thus suggest Windows OS dependencies, except for General > Empty Project, so that is the option I have chosen. 每个新项目选项似乎都说“ Windows”,因此建议Windows操作系统依赖项(“常规”>“空项目”除外),因此这是我选择的选项。

I am trying to write a static library with some functions. 我正在尝试编写具有某些功能的静态库。 Here's a simple example of what I'm trying to do. 这是我要尝试做的一个简单示例。

//Lib1.h (Project A)
#pragma once
#ifndef LIB_H
namespace Lib1
{
    double Add(double, double);
}

//Lib1.cpp (Project A)
#include "Lib1.h"
namespace Lib1
{
    double Add(double a, double b)
    {
        return a + b;
    }
}

//Main.cpp (Project B)
#include "Lib1.h"

using namespace Lib1;

int main()
{
    double c = Add(1, 2);
}

Project A compiles without warning/error. 项目A编译时没有警告/错误。 I changed the Project Properties > General > Configuration Type to be Static Library (.lib), but I cannot actually find the compiled .lib file in the project directory, just an .obj file. 我将项目属性>常规>配置类型更改为静态库(.lib),但实际上无法在项目目录中找到已编译的.lib文件,而只是一个.obj文件。

When I try and build Project B it throws the following error message. 当我尝试构建Project B时,它将引发以下错误消息。

"LNK2019 unresolved external symbol "double __cdecl FrameTransformations::Add(double,double)" (?Add@FrameTransformations@@YANNN@Z) referenced in function _main FrameTransformationTests S:\\Projects\\Cpp\\Code\\FrameTransformations\\FrameTransformationTests\\Main.obj 1" 在函数_main FrameTransformationTests S:\\ Projects \\ Cpp \\ Code \\ FrameTransformations \\ FrameTransformations \\ FrameTransformations \\ FrameTransformationsTests \\ Main.obj中引用的“ LNK2019无法解析的外部符号“ double __cdecl FrameTransformations :: Add(double,double)”(?Add @ FrameTransformations @@ YANNN @ Z) 1“

I've had a look and it seems that project B can't find the Add function from project A, although I get auto-completion for the function and arguments through Intellisense (is this just because it can find the Lib1.h file ok?). 我看了一下,似乎项目B找不到项目A的Add函数,尽管我通过Intellisense获得了自动完成的函数和参数(这仅仅是因为它可以找到Lib1.h文件。 ?)。

I have tried adding in the path to the Project A folder in the Project B Properties > C/C++ > General > Additional #using Directories (both to the Project folder and to the Debug folder where I expected the Lib1.lib file to be generated), but that hasn't worked. 我尝试在“项目B属性”>“ C / C ++”>“常规”>“其他#using目录”中添加到“项目A”文件夹的路径(到“项目”文件夹和“ Debug”文件夹,我都希望生成Lib1.lib文件) ),但这没有用。

Can someone please point me to what I've done wrong, this has had me stumped for a couple of hours now. 有人可以指出我做错了什么吗,这使我难过了几个小时。

Thank you. 谢谢。

.lib文件通常位于解决方案目录下,而不是更具体的项目目录下:在\\或可能的\\\\中查找。

When you #include a .h file, you are usually just including declarations into your code (exceptions apply, but not in this case). #include .h文件时,通常只在代码中包含声明 (例外适用,但在这种情况下不适用)。 These are separate from definitions which are placed into .cpp files. 这些与放置在.cpp文件中的定义是分开的。

By default your projects do not automatically shared any of this information with each other. 默认情况下,您的项目不自动共享任何的相互信息。 Your definitions nor declarations from Project A do not magically propagate to Project B, for example. 例如,您对项目A的定义或声明不会神奇地传播到项目B。

But as you've done here, you can reference declarations from Project A into Project B by #include ing header files. 但是,正如您在此处所做的那样,您可以通过#include头文件将项目A中的声明引用到项目B中。 These, however, are just declarations! 但是,这些只是声明! They just explain the existence of a function but don't implement it -- You need to add the definition of your Add function to Project B. 他们只是解释函数的存在而没有实现它-您需要将Add函数的定义Add到ProjectB。

The process of bringing such definitions together is called linking. 将这些定义放在一起的过程称为链接。 You need to link the definitions of Project A (the .lib, more precisely) into Project B. The way you do this changes with each version of Visual Studio just slightly, but it's usually under some "Linker Options" and some "Dependencies" tab/window. 您需要将项目A的定义(更确切地说是.lib)链接到项目B。每种Visual Studio版本的更改方式稍有不同,但通常在“链接器选项”和“依赖项”下标签/窗口。

You need to add reference of Project A into Project B->Project Properties as below, 您需要将Project A的引用添加到Project B-> Project Properties中,如下所示, 在此处输入图片说明

Also, you need to add a reference in like below 另外,您需要在下面添加参考

在此处输入图片说明

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

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