C++ and SQLite compilation error: "undefined reference to `sqlite3_open" - c++

I'm trying to get started with the SQLite on Eclipse CDT on Windows.
#include <iostream>
#include <string>
#include "sqlite3/sqlite3.h"
int main(){
sqlite3 *db;
sqlite3_stmt *res;
const char *errMSG;
const char *tail;
int error = sqlite3_open("test.db",&db);
if (error){
std::cout << "Could not open db." << std::endl;
sqlite3_close(db);
//system("pause");
return 0;
}
std::string query = "SELECT * FROM test";
error = sqlite3_prepare_v2(db, query.c_str(), query.length(), &res, &tail);
if (error != SQLITE_OK){
std::cout << "Could not prepare SQL" << std::endl;
sqlite3_close(db);
//system("pause");
return 0;
}
while (sqlite3_step(res) == SQLITE_ROW){
std::cout << "Name: " << sqlite3_column_text(res,0) << std::endl;
std::cout << "NUmber: " << sqlite3_column_text(res,1) << std::endl << std::endl;
}
sqlite3_finalize(res);
sqlite3_close(db);
//system("pause");
return 0;
}
This is the line that compiles the code:
g++ "-LD:\SQLite3\lib" -o sqliteTest00.exe main.o -lsqlite3 which include the lsqlite3. But when I build the project I will get the undefined reference to 'sqlite3_open" as well for 'sqlite3_close' and 'sqlite3_prepare_v2' and the rest of 'sqlite3_ commands. Any idea what is the problem with this file or linker?

Related

Try to connect Mysql but FAILED

I tried to connect MySQL.
But it seems like something goes wrong and I cannot get it.
I'm basically sending a simple SELECT statement to the database and checking if the operation was successful or not.
Unfortunately, this always outputs "Failed!"
#include <iostream>
#include <string>
#include <mysql/mysql.h>
using namespace std;
int main() {
const char *db = "test";
const char *server = "localhost";
const char *user = "root";
const char *password = "111222";
int port = 3306;
const char *sql = "select 1";
MYSQL *con = mysql_init(NULL);
// con = mysql_init(con);
if (con == NULL) {
cout << "Error:" << mysql_error(con) << endl;
exit(1);
}
MYSQL *new_con = mysql_real_connect(con, server, user, password, db, port, NULL, 0);
if (new_con != NULL) {
cout << "Successful!" << endl;
} else {
cout << "Failed!" << mysql_error(new_con) << endl;
}
int ret = mysql_query(new_con, sql);
if (ret != 0) {
cout << "error: " << mysql_error(new_con) << endl;
}
return 0;
}
Adapted from https://dev.mysql.com/doc/c-api/8.0/en/mysql-real-connect.html
MYSQL mysql;
mysql_init(&mysql);
if (!mysql_real_connect(&mysql,"localhost","root","111222","test",0,NULL,0))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(&mysql));
}
else {
cout << "Successfully connected." << endl;
}
What does this sample code print ?

Access violation during execution on CreateInstance function

I'm setting a program to create a COM connection from an ocx file.
I'm using visual studio 2019 with debug x86. It correctly generate the .tlh and .tli files.
My failed tests:
Use visualtudio 2017 and 2019
Use CoCreateInstance:
IMachine* machine = NULL;
// this gives the same error:
HRESULT hr2 = CoCreateInstance(CLSID_Machine, NULL, CLSCTX_INPROC_SERVER,
IID_IMachine,
reinterpret_cast<LPVOID*>(&machine));
Replace CLSID_Machine by the _uuidof(IMachine)
Replace CLSID_Machine by the LPCWSTR clsidString
This is a part of my code:
#import "lib.ocx" no_namespace , named_guids
HRESULT hr1 = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
try {
if (SUCCEEDED(hr1))
{
IMachinePtr machineCUPtr;
HRESULT hr2 = machineCUPtr.CreateInstance(CLSID_Machine, NULL,
CLSCTX_INPROC_SERVER); //Failure
if (FAILED(hr2))
{
std::cout << "Fail:" << hr2 << std::endl;
}
else {
std::cout << "Success: " << hr2 << std::endl;
}
}
else {
std::cout << "Fail CoInit: " << std::endl;
}
}
catch (_com_error& e)
{
std::cout << "COM ERROR catched " << std::endl;
std::cout << "Code = %08lx\n" << e.Error() << std::endl;
std::cout << "Meaning = %s\n" << e.ErrorMessage() << std::endl;
std::cout << "Source = %s\n" << (LPCSTR)e.Source() << std::endl;
std::cout << "Description = %s\n" << (LPCSTR)e.Description() << std::endl;
}
CoUninitialize();
}
I still have this error:
Exception raised to 0x00B20768 in myApp.exe : 0xC000000005 : Access
violation during execution at location 0x00B20768.
Edit:
'''
#include <iostream>
#import "libcom.ocx"
using namespace LIBCOM;
int main()
{
CoInitialize(NULL);
IMachinePtr machineCUPtr(__uuidof(Machine)); //Same error
machineCUPtr->MyMethod(parameters of the method);
CoUninitialize();
return 0;
}
''''

Connect to an online db in c++

I created my database in phpMyAdmin from godaddy.com.
But i can't seem to find a way to connect to it.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#include "cppconn/driver.h"
#include "cppconn/exception.h"
#include "cppconn/resultset.h"
#include "cppconn/statement.h"
#pragma comment(lib, "mysqlcppconn.lib")
const string server = "myWebsite";
const string username = "username";
const string password = "password";
int main()
{
sql::Driver *driver;
sql::Connection *dbConn;
sql::Statement *stmt;
sql::ResultSet *res;
try
{
driver = get_driver_instance();
}
catch (sql::SQLException e)
{
cout << "Could not get a database driver. Error message: " << e.what() << endl;
system("pause");
exit(1);
}
try
{
dbConn = driver->connect(server, username, password);
}
catch (sql::SQLException e)
{
cout << "Could not connect to database. Error message: " << e.what() << endl;
system("pause");
exit(1);
}
stmt = dbConn->createStatement();
try
{
stmt->execute("USE test");
res = stmt->executeQuery("SELECT * FROM users");
}
catch (sql::SQLException e)
{
cout << "SQL error. Error message: " << e.what() << endl;
system("pause");
exit(1);
}
sql::ResultSetMetaData *res_meta = res -> getMetaData();
int columns = res_meta -> getColumnCount();
while (res->next())
{
for (int i = 1; i <= columns; i++) {
cout << res->getString(i) << " | " ;
}
cout << endl;
}
delete res;
delete stmt;
delete dbConn;
return 0;
}
But i get this error:
Could not connect to database. Error message: Access denied for user 'username'#'IP' (using password: YES)
This code works fine with the local phpmyadmin databases.
So is there any different way to connect to databases that are hosted on a website?

Fake files "exist" after checking real files?

I have been programming C++ on Linux for a while, but recently moved to a windows 10 computer.
I managed to set up CodeBlocks with w64-mingw.
I have been trying to move programs from linux to windows and I'm having trouble with filenames. For example, I have code to check if files or directories exist, and to create directories. But I get weird results, if a file check comes back as true, then all subsequent file checks come back as true. I have example code, where test.txt and testdir are a file and directory that do not initially exist, but are created by the program. fail.txt and faildir never exist, but my program claims they exist AFTER creating test.txt and testdir. I've seen several questions about checking if files exist on Windows, but I've never run into behavior like this, and I'm not sure what's going on. Does windows fail to reinitialize something when GetFileAttributes() is called? Or have I missed something really basic?
main.cpp
#include <iostream>
#include <fstream>
#include "../include/FileChecker.h"
int main(){
FileChecker fc = FileChecker();
std::cout << "Test Start" << std::endl;
#ifdef _WIN32
std::cout << "OS is windows" << std::endl;
#endif // _WIN32
std::cout << std::endl;
std::cout << "Nothing should exist" << std::endl;
if(fc.file_exists("test.txt")){
std::cout << "test.txt exists." << std::endl;
}else{
std::cout << "test.txt does not exist." << std::endl;
}
if(fc.file_exists("fail.txt")){
std::cout << "fail.txt exists." << std::endl;
}else{
std::cout << "fail.txt does not exist." << std::endl;
}
if(fc.directory_exists("testdir")){
std::cout << "Directory testdir exists." << std::endl;
}else{
std::cout << "Directory testdir does not exist." << std::endl;
}
if(fc.directory_exists("faildir")){
std::cout << "Directory faildir exists." << std::endl;
}else{
std::cout << "Directory faildir does not exist." << std::endl;
}
std::cout << std::endl;
std::cout << "Creating test.txt" << std::endl;
std::ofstream test("test.txt");
test << "HELLO" << std::endl;
test.close();
std::cout << "Only test.txt should exist" << std::endl;
if(fc.file_exists("test.txt")){
std::cout << "test.txt exists." << std::endl;
}else{
std::cout << "test.txt does not exist." << std::endl;
}
if(fc.file_exists("fail.txt")){
std::cout << "fail.txt exists." << std::endl;
}else{
std::cout << "fail.txt does not exist." << std::endl;
}
if(fc.directory_exists("testdir")){
std::cout << "Directory testdir exists." << std::endl;
}else{
std::cout << "Directory testdir does not exist." << std::endl;
}
if(fc.directory_exists("faildir")){
std::cout << "Directory faildir exists." << std::endl;
}else{
std::cout << "Directory faildir does not exist." << std::endl;
}
std::cout << std::endl;
std::cout << "Creating directory testdir" << std::endl;
if(fc.create_directory("testdir")){
std::cout << "Creation Success" << std::endl;
}else{
std::cout << "Creation Failed" << std::endl;
}
std::cout << "Only testdir should exist" << std::endl;
if(fc.directory_exists("testdir")){
std::cout << "Directory testdir exists." << std::endl;
}else{
std::cout << "Directory testdir does not exist." << std::endl;
}
if(fc.directory_exists("faildir")){
std::cout << "Directory faildir exists." << std::endl;
}else{
std::cout << "Directory faildir does not exist." << std::endl;
}
return 0;
}
FileChecker.h
#ifndef FILECHECKER_H
#define FILECHECKER_H
#ifdef _WIN32
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <direct.h>
#endif // _WIN32
#include <string>
class FileChecker
{
public:
FileChecker();
virtual ~FileChecker();
bool file_exists(std::string filename);
bool directory_exists(std::string dirname);
bool create_file(std::string filename);
bool create_directory(std::string dirname);
protected:
private:
};
#endif // FILECHECKER_H
FileChecker.cpp
#include "../include/FileChecker.h"
FileChecker::FileChecker(){
//ctor
}
FileChecker::~FileChecker(){
//dtor
}
#ifdef _WIN32
bool FileChecker::file_exists(std::string filename){
static LPCTSTR szPath = TEXT(filename.c_str());
DWORD dwAttrib = GetFileAttributes(szPath);
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
#endif // _WIN32
#ifdef _WIN32
bool FileChecker::directory_exists(std::string dirname){
static LPCTSTR szPath = TEXT(dirname.c_str());
DWORD dwAttrib = GetFileAttributes(szPath);
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
#endif // _WIN32
#ifdef _WIN32
bool FileChecker::create_directory(std::string dirname){
static LPCTSTR szPath = TEXT(dirname.c_str());
return(CreateDirectory(szPath, NULL));
}
#endif // _WIN32
Output
You should remove all static keyword in your functions.
bool FileChecker::file_exists(std::string filename){
static LPCTSTR szPath = TEXT(filename.c_str()); // <--- [*]
DWORD dwAttrib = GetFileAttributes(szPath);
when file_exists function is called first time, szPath variable is created and initialized pointing to array of characters of filename. When you call file_exists second time, value of szPath is still the same, and points to invalid data (keeps pointer to data of filename object, which was deleted after calling file_exists first time).
You should read about static variables in functions.
Your code here:
bool FileChecker::file_exists(std::string filename){
static LPCTSTR szPath = TEXT(filename.c_str());
DWORD dwAttrib = GetFileAttributes(szPath);
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
TEXT simply casts, it does not perform any sort of conversion. Make it the following instead:
bool FileChecker::file_exists(std::string filename)
{
DWORD dwAttrib = GetFileAttributesA(filename.c_str());
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}

pthread fails to link without a call in main

I've been attempting to use angelscript and I keep getting linker errors, however if I put a reference to pthread_exit(NULL) at the end of my main function it builds, links, and runs normally.
Here is the code in question:
#include <iostream>
#include <angelscript.h>
#include <cassert>
// This is used for AngelScript error messages
void MessageCallback(const asSMessageInfo &msg)
{
const char *type = "ERR ";
if( msg.type == asMSGTYPE_WARNING )
{
type = "WARN";
}
else if( msg.type == asMSGTYPE_INFORMATION )
{
type = "INFO";
}
std::cout << msg.section << " (" << msg.row << ", " << msg.col << ") : " << type << " : " << msg.message << std::endl;
}
int main(void)
{
asIScriptEngine* engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
int r = engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);
assert(r >= 0);
std::cout << "Testing" << std::endl;
engine->Release();
pthread_exit(NULL);
return 0;
}
and this is the command line:
g++ -Wl,-O1,--sort-common,--as-needed,-z,relro -Wl,-O1 -o fib main.o -langelscript -lpthread
any ideas?