Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7080939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:51:24+00:00 2026-05-28T06:51:24+00:00

I have c++ code to connect to and use mysql: #include <iostream> #include <mysql/mysql.h>

  • 0

I have c++ code to connect to and use mysql:

#include <iostream>
#include <mysql/mysql.h>

using namespace std;

MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;

int main() {
mysql_init(&mysql);

connection = mysql_real_connect(&mysql,"localhost","root","pass","cpp_data",0,0,0);
if (connection == NULL) {
cout << mysql_error(&mysql) << endl;
return 1;
}
return 0;
}

When I compile, I get test.cpp:(.text+0x11): undefined reference to 'mysql_init' and the same error for mysql_real_connect and mysql_error.

I have tried gcc -o test -L/usr/lib/mysql -lmysqlclient test.cpp, but this give me the following errors:

test.cpp:(.text+0x7a): undefined reference to `std::cout'
test.cpp:(.text+0x7f): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
test.cpp:(.text+0x87): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' etc.

I checked mysql.h and the definition of mysql_real_connect is:

MYSQL *     STDCALL mysql_real_connect(MYSQL *mysql, const char *host,
                       const char *user,
                       const char *passwd,
                       const char *db,
                       unsigned int port,
                       const char *unix_socket,
                       unsigned long clientflag);

I have no problem when I don’t use these mysql functions. Is there another option to solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:51:25+00:00Added an answer on May 28, 2026 at 6:51 am

    You need to compile and link C++ code with g++, not gcc. Otherwise the C++ standard libraries will not get linked properly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have this code use for printing a table in mysql database ,
I'm trying to use odbc to connect some erlang code to a mysql server.
I have this code to connect to Active Directory and get all the groups
I have to create an sample code that connect to a socket server, and
I have a legacy PHP/MySQL app that calls mysql_connect(). Tons of existing downstream code
I have some Java code that connects to an Oracle database using DriverManager.getConnection(). It
I have this mySQL code that connects to my server. It connects just fine:
So I have a class.. In its constructor I include the code which connects
I have installed mySQL using sudo apt-get install mySQL-server then i have installed libmysqlclient15-dev
I am using JBOSS AS 7.0.2. I want to create a connection to mysql

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.