0

I am trying to connect to a sql database using ODBC in c++ and I can't call any of the functions in sql.h file: Here is my test code:

#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <stdlib.h>


using namespace std;
int main(){
    SQLHANDLE SQLEnvHandle;
    SQLRETURN myRet = SQLAllocStmt(NULL,NULL);
    cout<<cos(3.0);
    return 0;
}

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe générer le fichier actif",
            "command": "C:\\Users\\caronfe\\AppData\\Local\\msys2\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compilateur : C:\\Users\\caronfe\\AppData\\Local\\msys2\\mingw64\\bin\\g++.exe"
        }
    ]
}

I tried to compile with g++, getting this error: undefined reference to `SQLAllocStmt'

When I switch one of the NULL parameters to an integer, the compiler gives me this error instead: invalid conversion from 'int' to 'SQLHDBC'

So the compiler find the function, but still throws an undefined reference error. The same behavior happens for other sql.h functions, but not for any other import. Config: I am on windows 10 and mingw64 is installed for the user only I am using VSCode editor (Intellisense also finds the functions...) Other codes not using sql.h compiles and execute perfectly Compiler: g++ (Rev1, Built by MSYS2 project) 11.3.0

Félix
  • 1
  • 1
  • 1
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – ChrisMM May 12 '22 at 16:18
  • You probably need to link to some library. Perhaps `odbc32.lib` – drescherjm May 12 '22 at 16:19
  • Building a C++ program is done in multiple steps: 1) Edit the source code; Compile the source code into an object file (this is the step where the preprocessor includes header files); 3) Link object files with libraries to create the final executable program. Your error happens in the last step, probably because you don't link with the ODBC library. – Some programmer dude May 12 '22 at 16:20
  • 2
    ***Intellisense also finds the functions*** Intellisense in VSCode is not a good indicator of compile time problems. The settings for include paths, what files to build and what to link are completely independent of the settings in c_cpp_properties.json which configures intellisense. – drescherjm May 12 '22 at 16:22
  • True, but how could the compiler give me an error on the parameters of the function if it cannot find it? – Félix May 12 '22 at 16:32
  • Your problem is likely a linker problem in your `tasks.json`. You need to add additional args: to link to the odbc libraries. – drescherjm May 12 '22 at 16:35
  • The compiler can find the *declaration* of the function, as it's in the included `sql.h` header file. The declaration only tell the compiler that the function exists somewhere, and what the type of its arguments are, and what it returns. The *definition* is the implementation of the function, and it's in a library that you need to link with. – Some programmer dude May 12 '22 at 16:44
  • If you haven't read [this GCC with VSCode tutorial](https://code.visualstudio.com/docs/cpp/config-mingw) I recommend you do it now. Pay close attention to the difference in purpose between the `c_cpp_properties.json` and `tasks.json` configuration files. – Some programmer dude May 12 '22 at 16:45
  • Thanks, but the sql.h is located in the standard include path:"C:\Users\caronfe\AppData\Local\msys2\mingw64\include\sql.h",where should be the definition? – Félix May 12 '22 at 16:57
  • ***where should be the definition?*** In some .lib file. On the second comment I told you to try `odbc32.lib` – drescherjm May 12 '22 at 17:38
  • It is not on my computer, but I do have odbc32.dll – Félix May 12 '22 at 18:15

0 Answers0