#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    11
    Rep Power
    0

    Question LNK2001 errors trying to use


    I am developing an application using vc++ 6.0 in which trying to connect mysql by using the mysql_real_connect command. My program compiles okay, but I get LNK2001 errors. I have c:/mysql/include in include path and c:/mysql/lib in libpath. Does anyone have any idea what I could be doing wrong ( I have included my code below)

    In addition, I have noticed that if isolate the mysql functions in a class of their own they compile and link okay but when I then use the fuctions in the isolated class from another class I get the linker errors once more.

    Also I used a tutorial to try and do this, and the sample code they provide works fine. I tried for hours to figure out what they did differntly with no luck. Here is the link:
    http://www.arcanthea.com/opensource/...tut-us-pdf.zip

    What am I doing wrong?

    Thanks in advance!

    ERRORS:
    RelCalcDlg.obj : error LNK2001: unresolved external symbol _mysql_real_connect@32
    RelCalcDlg.obj : error LNK2001: unresolved external symbol _mysql_init@4
    Debug/RelCalc.exe : fatal error LNK1120: 2 unresolved externals


    Isolated Class // compiles and links with no problem:
    #include "StdAfx.h"
    #include <mysql.h>

    class test
    {

    public:
    MYSQL * data;
    CString mhost;
    CString muser;
    CString mpass;
    UINT mport;

    BOOL testFunc()
    {
    mhost = "localhost";
    muser = "";
    mpass = "";
    mport = 3306;

    if((data = mysql_init(NULL)) &&
    mysql_real_connect(data, mhost, muser, mpass, NULL, mport, NULL, 0))
    {
    return TRUE;
    }
    return FALSE;
    }

    };

    my original class that gets the LNK2001 errros:
    // RelCalcDlg.cpp : implementation file
    //

    #include "stdafx.h"
    #include "RelCalc.h"
    #include "RelCalcDlg.h"
    #include "DlgProxy.h"

    //new
    //#include "NI.h"
    #include <Mysql.h>
    #include "ConnectionDlg.h"

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    /////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App About

    class CAboutDlg : public CDialog
    {public: CAboutDlg();
    ....};

    ...

    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // CRelCalcDlg dialog

    IMPLEMENT_DYNAMIC(CRelCalcDlg, CDialog);

    CRelCalcDlg::CRelCalcDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CRelCalcDlg::IDD, pParent)
    {
    IsConnected = FALSE;
    //{{AFX_DATA_INIT(CRelCalcDlg)
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_pAutoProxy = NULL;
    }

    CRelCalcDlg::~CRelCalcDlg()
    {
    // If there is an automation proxy for this dialog, set
    // its back pointer to this dialog to NULL, so it knows
    // the dialog has been deleted.
    if (m_pAutoProxy != NULL)
    m_pAutoProxy->m_pDialog = NULL;
    }

    void CRelCalcDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CRelCalcDlg)
    DDX_Control(pDX, IDC_DICONNECT, m_Disconnect);
    DDX_Control(pDX, IDC_CONNECT, m_Connect);
    DDX_Control(pDX, IDC_INFOS, m_MyInfosBtn);
    //}}AFX_DATA_MAP
    }

    BEGIN_MESSAGE_MAP(CRelCalcDlg, CDialog)
    //{{AFX_MSG_MAP(CRelCalcDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_WM_CLOSE()
    ON_BN_CLICKED(IDC_CONNECT, OnConnect)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // CRelCalcDlg message handlers

    BOOL CRelCalcDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    }

    // Set the icon for this dialog. The framework does this automatically
    // when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here


    return TRUE; // return TRUE unless you set the focus to a control
    }

    void CRelCalcDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {...}

    void CRelCalcDlg::OnPaint()
    {...}

    HCURSOR CRelCalcDlg::OnQueryDragIcon()
    {...}

    void CRelCalcDlg::OnClose()
    {...}

    void CRelCalcDlg::OnOK()
    {...}

    void CRelCalcDlg::OnCancel()
    {...}

    BOOL CRelCalcDlg::CanExit()
    {
    if (m_pAutoProxy != NULL)
    {
    ShowWindow(SW_HIDE);
    return FALSE;
    }
    return TRUE;
    }


    void CRelCalcDlg::OnConnect()
    {
    // TODO: Add your control notification handler code here
    CConnectionDlg dlg;

    if(dlg.DoModal()==IDOK)
    {
    myHost = dlg.m_Host;
    myUser = dlg.m_User;
    myPassword = dlg.m_Password;
    myPort = dlg.m_Port;

    if(!DBConnection(myHost, myUser, myPassword, myPort))
    {
    return;
    }

    if(FALSE)
    {
    MessageBox("Cant get the databases from the server");
    }

    m_MyInfosBtn.EnableWindow(TRUE);
    m_Connect.EnableWindow(FALSE);
    m_Disconnect.EnableWindow(TRUE);
    }
    }

    BOOL CRelCalcDlg::DBConnection(CString &mHost, CString &mUser, CString &mPass, UINT mPort)
    {
    //CAUSES LINKER ERRORS ...
    test mytest;
    BOOL result = mytest.testFunc();
    if(result)
    {
    m_Disconnect.EnableWindow(TRUE);
    m_Connect.EnableWindow(FALSE);
    IsConnected = TRUE;
    return TRUE;
    }
    else
    return FALSE;

    //CAUSES LINKER ERRORS ...
    /* if ( (myData = mysql_init(NULL)) &&
    mysql_real_connect(myData, mHost, mUser, mPass, NULL, mPort, NULL, 0))
    {
    m_Disconnect.EnableWindow(TRUE);
    m_Connect.EnableWindow(FALSE);
    IsConnected = TRUE;
    return TRUE;
    }
    */
    return FALSE;
    }
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    11
    Rep Power
    0

    Lightbulb i figured it out


    I figured out my mistake. In addtion to adding c:/mysql/include in include path and c:/mysql/lib in libpath I also needed to add libmysql.lib to project ->settings->link tab->object/library modules.

    I noticed after I posted that my second paragraph was not true, the only way the linker didnt complain is if I never used the sql commands.

IMN logo majestic logo threadwatch logo seochat tools logo