1

i have a project in Netbeans to connect to Mysql which is in my Raspberry pi card. so the example code i found is:

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

// <editor-fold defaultstate="collapsed" desc="comment">
using namespace std; // </editor-fold>


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

#define HOST "myhost" //  
#define USER "mysuer" // 
#define PASSWD "mypwd" // 
#define DB "mydb"

int main()
{
//initialize database connection
    mysql_init(&mysql);

// the three zeros are: Which port to connect to, which socket to connect to 
// and what client flags to use.  unless you're changing the defaults you only need to put 0 here
    connection = mysql_real_connect(&mysql,HOST,USER,PASSWD,DB,0,0,0); 
// Report error if failed to connect to database
    if (connection == NULL) {
        cout << mysql_error(&mysql) << endl;
        return 1;
    }
//Send query to database
        query_state = mysql_query(connection, "select * from settings");
// store result
        result = mysql_store_result(connection);
       while ( ( row = mysql_fetch_row(result)) != NULL ) {
// Print result, it prints row[column_number])
        cout << row[0] << "\t" << row[1] << endl;
        }
    return 0;
}

when i run this code i have this error:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/home/pi/NetBeansProjects/Test1'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/test1
make[2]: Entering directory '/home/pi/NetBeansProjects/Test1'
mkdir -p dist/Debug/GNU-Linux-x86
g++     -o dist/Debug/GNU-Linux-x86/test1 build/Debug/GNU-Linux-x86/Test1.o -lbcm2835 `mysql_config --cflags` `mysql_config --libs`
/usr/bin/ld: cannot find -lbcm2835
collect2: ld returned 1 exit status
nbproject/Makefile-Debug.mk:62: recipe for target 'dist/Debug/GNU-Linux-x86/test1' failed
make[2]: *** [dist/Debug/GNU-Linux-x86/test1] Error 1
make[2]: Leaving directory '/home/pi/NetBeansProjects/Test1'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/home/pi/NetBeansProjects/Test1'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 3s)

Can you help me, thank you

  • You seem to be building against a Linux-x86 target. This will not work on a raspberry Pi, where the architecture is armv6 (or armv7 if you have an RPI2). The actual fail seems to be on linking a library called "bcm2835" which cannot be found by the linker. – Phil B. Sep 30 '15 at 18:07
  • 1
    @PhilB It is odd the directory path includes that (GNU-Linux-x86), although if it's being built on the pi it's being built with an ARM compiler, and unless there's asm in there somewhere it will be fine. libbcm2835 is actually a pi-specific library (see my answer). – goldilocks Sep 30 '15 at 18:15

1 Answers1

2

/usr/bin/ld: cannot find -lbcm2835

You need to install this library (libbcm2835), which is used to access the GPIO pins on Raspberry Pi A/B/+ models; the current version should also work on the Pi 2 (technically, a bcm2836).

You do not refer to needing the GPIOs, just MySQL, so if you were unaware the example project does this, now you are.

Documentation for libbcm2835 is here, and there is a link to the source download at the top (4th paragraph). As far as I am aware, it is not available as a binary package anywhere.

If you are new to installing libraries from source, after you run sudo make install, be sure to run sudo ldconfig so the linker can find the library.

goldilocks
  • 58,859
  • 17
  • 112
  • 227
  • Hi thank you for answers, i downloaded the bcm2835 library but i don't know how to remove the GNU linux x 86 and change it to ARM of raspberry pi – rpiprojects berry Sep 30 '15 at 18:39
  • Don't worry about it. I would guess someone left that in there (or used it in the first place) by mistake; it's just a directory path in the source package. It is extremely unlikely there's any actual assembler code in such a relatively high level project. C and C++ themselves are completely portable. If you are using the compiler on the pi, it will be compiled for the pi. Since libbcm2835 is being used, it is a safe bet it's been tested for that purpose too. – goldilocks Sep 30 '15 at 18:49
  • For bcm2835 it's ok but my code still got error for every line of my code for example for the sixth line i have this : Unable to resolve identifier MYSQL_RES. – rpiprojects berry Oct 01 '15 at 01:48
  • Maybe you should look for a better example to learn from? Keep in mind it doesn't have to be written for the raspberry pi -- anything POSIX compliant will work on Raspbian and the other GNU/Linux pi distros. Which is to say, pretty much anything that doesn't also clearly involve MS Windows. Looks like MySQL has an official C++ API with documentation. There's literally 1000+ questions about it on Stack Overflow as well... – goldilocks Oct 01 '15 at 02:02
  • ...If you restrict yourself to searches and material that reference the Raspberry Pi when you don't have to, you are kind of tying both hands behind your back. C++ is C++. MySQL is MySQL. They are the same whether you're developing on a pi, your laptop, etc. The only platform specific details will be little things that aren't standardized in C++ (which isn't much in this context), and those things will be the same across linux, at least, which has been around a pretty long time. – goldilocks Oct 01 '15 at 02:02
  • i resolved this problem by encoding the cpp file inUTF-8 without BOM, all errors disappeared – rpiprojects berry Oct 01 '15 at 02:07