简体   繁体   中英

Read ini file C++ error

I have an ini file that has this info: and this is my code:

#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. 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?

LNK2019 typically has one of the following reasons:

  • You forgot to link the proper lib file for your library (the ini reader in this case).
  • You linked the wrong lib file for your library (eg static vs. dynamic linking).
  • You forgot to add a cpp file for your library to your project.
  • You're mixing precompiled code that's meant for different platforms (like x86 and x64).

Edit: In this particular instance, you'll have to add the file IniReader.cpp to your project, as it provides the missing functions.

Use Windows API like GetPrivateProfileString and WritePrivateProfileString for reading .ini files. 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)
  • Drag all files of inih into your project in Visual Studio 2008 .
  • Then press build button.

The error's reason is like @Mario said. You just compiled your cpp file, but not compile other cpp files that is necessory for your project. So you also need to compile all .cpp files in inih .

Note: I think you should read this to learn more about the compiling and linking of C.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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