Sign in to follow this  
ShinkaFudan

PostGreSQL / libpq linking errors

Recommended Posts

ShinkaFudan    100
I have installed PostGreSQL and libpq for SQL in C++ on my machine. I am currently trying to use it in a project in Visual Studio 2008 and am getting the following errors.

[quote]Error 1 error LNK2019: unresolved external symbol _PQfinish referenced in function "bool __cdecl ConnectToDatabase(struct pg_conn * &,char *,char *,char *,char *,char *,char *,char *)" (?ConnectToDatabase@@YA_NAAPAUpg_conn@@PAD111111@Z)
Error 2 error LNK2019: unresolved external symbol _PQstatus referenced in function "bool __cdecl ConnectToDatabase(struct pg_conn * &,char *,char *,char *,char *,char *,char *,char *)" (?ConnectToDatabase@@YA_NAAPAUpg_conn@@PAD111111@Z)
Error 3 error LNK2019: unresolved external symbol _PQsetdbLogin referenced in function "bool __cdecl ConnectToDatabase(struct pg_conn * &,char *,char *,char *,char *,char *,char *,char *)" (?ConnectToDatabase@@YA_NAAPAUpg_conn@@PAD111111@Z)
Error 4 error LNK2019: unresolved external symbol _PQgetvalue referenced in function "bool __cdecl GetValue(struct pg_conn *,char *,char *,char *,int &)" (?GetValue@@YA_NPAUpg_conn@@PAD11AAH@Z)
Error 5 error LNK2019: unresolved external symbol _PQclear referenced in function "bool __cdecl GetValue(struct pg_conn *,char *,char *,char *,int &)" (?GetValue@@YA_NPAUpg_conn@@PAD11AAH@Z)
Error 6 error LNK2019: unresolved external symbol _PQerrorMessage referenced in function "bool __cdecl GetValue(struct pg_conn *,char *,char *,char *,int &)" (?GetValue@@YA_NPAUpg_conn@@PAD11AAH@Z)
Error 7 error LNK2019: unresolved external symbol _PQresultStatus referenced in function "bool __cdecl GetValue(struct pg_conn *,char *,char *,char *,int &)" (?GetValue@@YA_NPAUpg_conn@@PAD11AAH@Z)
Error 8 error LNK2019: unresolved external symbol _PQexec referenced in function "bool __cdecl GetValue(struct pg_conn *,char *,char *,char *,int &)" (?GetValue@@YA_NPAUpg_conn@@PAD11AAH@Z)[/quote]


These errors do not occur when I load the same project onto my computer at home. Additionally, I have the following code in the header of the file that gives all of these errors.

[quote]#pragma comment(lib, "libpq.lib")
#include <libpq-fe.h>[/quote]

libpq.lib is in the project folder and should be loadable without an issue.

libpq_fe.h seems to exist and be findable because it isn't giving an error saying it can't find it, and when I right click and go to the file, it has no trouble going right to it. I made sure that the folder that contains it is in my project includes as well.

I made sure I had the latest version from the PostGreSQL website. Everything seems to check out, but it is obvious that I have set something up incorrectly. Has anyone had this issue, or does anyone know what the issue might be?

Share this post


Link to post
Share on other sites
Toadhead    244
I'm having the same issues. I'm using Visual studio 2010 express.
I set up the include and lib directory and included the header file and added the libpq.lib to the additional lib files.
I'm not sure what I'm doing wrong, here is my source code:

[b]SQLManager.cpp[/b]

[code]
#include "SQLManager.h"
#include <fstream>

SQLManager::SQLManager()
{
}

bool SQLManager::Connect()
{

m_pgConnection = PQconnectdb("user=postgres password=postgres dbname=testdb hostaddr=127.0.0.1 port=5433");

if(PQstatus(m_pgConnection) != CONNECTION_OK)
{
// Handle no connection error
SqlLog("Can't connect to database, please try again.");
Disconnect();
return false;
}


return true; //return true on succes
}

void SQLManager::Disconnect()
{
PQfinish(m_pgConnection);
}

void SQLManager::CreateDB(std::string dbname)
{
}

void SQLManager::DeleteDB(std::string dbname)
{
}

void SQLManager::ExecuteSqlString(std::string sqlstring)
{
}

void SQLManager::ExecuteSqlFile(std::string sqlfile)
{
}

void SqlLog(std::string error)
{
std::ofstream myStream;
myStream.open("BlockRain2.log", std::ios_base::app);
myStream << "SQL:\n" << "--------------\n" << error << "\n" << std::endl;;
myStream.close();
}
[/code]
[b]SQLManager.h[/b]

[code]
#pragma once
#include <string>
#include <libpq-fe.h>


class SQLManager
{
public:
SQLManager();

bool Connect();
void Disconnect();

void CreateDB(std::string dbname);
void DeleteDB(std::string dbname);

void ExecuteSqlString(std::string sqlstring);
void ExecuteSqlFile(std::string sqlfile);

void SqlLog(std::string error);

private:
PGconn *m_pgConnection;


};
[/code]


The errors:

1>------ Build started: Project: Block Rain 2, Configuration: Debug Win32 ------
1> SQLManager.cpp
1>SQLManager.obj : error LNK2019: unresolved external symbol "public: void __thiscall SQLManager::SqlLog(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?SqlLog@SQLManager@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: bool __thiscall SQLManager::Connect(void)" (?Connect@SQLManager@@QAE_NXZ)
1>SQLManager.obj : error LNK2019: unresolved external symbol _PQstatus referenced in function "public: bool __thiscall SQLManager::Connect(void)" (?Connect@SQLManager@@QAE_NXZ)
1>SQLManager.obj : error LNK2019: unresolved external symbol _PQconnectdb referenced in function "public: bool __thiscall SQLManager::Connect(void)" (?Connect@SQLManager@@QAE_NXZ)
1>SQLManager.obj : error LNK2019: unresolved external symbol _PQfinish referenced in function "public: void __thiscall SQLManager::Disconnect(void)" (?Disconnect@SQLManager@@QAEXXZ)
1>..\Test\Block Rain 2.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Share this post


Link to post
Share on other sites
Toadhead    244
Update:

I have both postgreSQL 8.4 and 9.0 installed.
I tried to use the 9.0 lib and includes but that didn't work. After changing the paths to the 8.4 include and libs it does compile.
Can anyone explain this?

Greetings,
Rob


Edit/Update:
I've found a solution.
I uninstalled everything (both 8.4 and 9.0), and re-installed the win32 version of 9.0 (previously downloaded the win64 version of the windows binaries) and after this installation everything works fine. So my guess is the win64 version of 9.0 somehow doesn't work, even though I am running a 64bit version of windows. I'm a little bit confused about it but it works now.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this