简体   繁体   English

读取ini文件C ++错误

[英]Read ini file C++ error

I have an ini file that has this info: and this is my code: 我有一个包含以下信息的ini文件:这是我的代码:

#include "stdafx.h"
#include "iostream"
#include <stdio.h>
#include "IniReader.h"
#include "INIReader.h"

using namespace std;
int main(int argc, char * argv[])
{
    INIReader reader("C:\SampleFile.ini");

    if (reader.ParseError() < 0) {
   cout << "Can't load 'test.ini'\n";
        return 1;
    }
    std::cout << "Config loaded from 'test.ini': version="
              << reader.GetInteger("info", "CaptureDuration", -1) << "CaptureDuration"<<"\n"
              << reader.Get("info", "DayStart", `enter code here`"UNKNOWN") << ", email="; 


cin.get();
 return 0;   
}

I also attached the INIReader header file that I found from this link: https://code.google.com/p/inih/source/browse/trunk/cpp/INIReader.h I want the code to print the integer and the string values that it got from the ini file. 我还附加了从此链接中找到的INIReader头文件: https : //code.google.com/p/inih/source/browse/trunk/cpp/INIReader.h我希望代码打印整数和字符串从ini文件获得的值。 but I am getting Linker errors. 但我收到链接器错误。 How can I fix it? 我该如何解决?

Errors:
,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?Get@INIReader@@QAE?AV?$

1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall INIReader::ParseError(void)" (?ParseError@INIReader@@QAEHXZ) referenced in function _main
@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main

1>C:\Users\Owner1\Documents\Visual Studio 2008\Projects\inireader\Debug\inireader.exe : fatal error LNK1120: 4 unresolved externals

I do't see any lib file to link to this project, where should I get the lib files? 我看不到任何链接到此项目的lib文件,我应该在哪里获取lib文件?

LNK2019 typically has one of the following reasons: LNK2019通常具有以下原因之一:

  • You forgot to link the proper lib file for your library (the ini reader in this case). 您忘记为您的库链接正确的lib文件(在这种情况下为ini阅读器)。
  • You linked the wrong lib file for your library (eg static vs. dynamic linking). 您为库链接了错误的lib文件(例如,静态链接与动态链接)。
  • You forgot to add a cpp file for your library to your project. 您忘记将库的cpp文件添加到项目中。
  • You're mixing precompiled code that's meant for different platforms (like x86 and x64). 您正在混合用于不同平台(例如x86和x64)的预编译代码。

Edit: In this particular instance, you'll have to add the file IniReader.cpp to your project, as it provides the missing functions. 编辑:在此特定实例中,您必须将文件IniReader.cpp添加到您的项目中,因为它提供了缺少的功能。

Use Windows API like GetPrivateProfileString and WritePrivateProfileString for reading .ini files. 使用Windows API(如GetPrivateProfileStringWritePrivateProfileString读取.ini文件。 It works fine and does not require any libraries. 它工作正常,不需要任何库。 I used it for decades. 我用了几十年。

The simplest way is: 最简单的方法是:

  • Copy all files of inih into your project directory (I think it is C:\\Users\\Owner1\\Documents\\Visual Studio 2008\\Projects\\inireader) inih所有文件复制到您的项目目录中(我认为它是C:\\ Users \\ Owner1 \\ Documents \\ Visual Studio 2008 \\ Projects \\ inireader)
  • Drag all files of inih into your project in Visual Studio 2008 . inih所有文件inih Visual Studio 2008中的项目中。
  • Then press build button. 然后按构建按钮。

The error's reason is like @Mario said. 错误的原因就像@Mario所说。 You just compiled your cpp file, but not compile other cpp files that is necessory for your project. 您只是编译了cpp文件,而没有编译项目需要的其他cpp文件。 So you also need to compile all .cpp files in inih . 因此,您还需要在inih编译所有.cpp文件。

Note: I think you should read this to learn more about the compiling and linking of C. 注意:我认为您应该阅读此书以了解有关C的编译和链接的更多信息。

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

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