Search code examples
ccommand-line-interfacemacos-high-sierramysql-connector-c

How can I compile mysql-connector-c for macOS 10.13.x using C?


Synopsis

I have a small c program, it's a standard "hello world" application to test I can access the mysql connection driver accordingly:

#include <stdio.h>
#include "mysql.h"

int main(int argc, const char **argv)
{
    printf("MySQL client version: %s\n", mysql_get_client_info());

    return 0;
}

When I compile this using the following; I get an error:

gcc src/main.c \
    -Wall \
    -Ilib/mysql-connector-c-6.1.11-macos10.12-x86_64/include \
    -o bin/test

Undefined symbols for architecture x86_64: "_mysql_get_client_info", referenced from: _main in main-59d4fb.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

I have then attempted to link mysql with the following:

gcc src/main.c \
    -Wall \
    -Ilib/mysql-connector-c-6.1.11-macos10.12-x86_64/include \
    -Llib/mysql-connector-c-6.1.11-macos10.12-x86_64/mysql-connector-c-6.1.11-src/libmysql \
    -lmysql \
    -o bin/test

ld: library not found for -lmysql clang: error: linker command failed with exit code 1 (use -v to see invocation)

It's worth noting I am using not c++ and I am not using XCode just Vim and gcc as can be seen above.

Alternative attempt

I then decided perhaps i've done something wrong with compiling the source from MySQL This particular link and so I deleted the lib directory within my application and installed them via homebrew:

I tried brew install mysql-connector-c I also received the same errors as above despite changing the -I -L -l accordingly the new paths /usr/local/Cellar/... and exactly the same problems were occurring.

Conclusion

I could not find any .c files in any of the compiled mysql libraries, I did find a libmysql inside the source i compiled and attempted to link that which also failed. I personally feel it's a linking problem I just don't know what or how, if anyone could help me with this I would really appreciate it.


Anyone who may want to see the response of -v please see below:

Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.13.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-cpu penryn -target-linker-version 305 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -I lib/mysql-connector-c-6.1.11-macos10.12-x86_64/include -I/usr/local/include -Wall -fdebug-compilation-dir /Users/ash/Projects/samp-db2 -ferror-limit 19 -fmessage-length 173 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.13.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/v7/_rm0d0qx2r32ln2f7_dpnrw40000gn/T/main-3d5e25.o -x c src/main.c
clang -cc1 version 9.0.0 (clang-900.0.39.2) default target x86_64-apple-darwin17.3.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
lib/mysql-connector-c-6.1.11-macos10.12-x86_64/include
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.0.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.13.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -o bin/cmig /var/folders/v7/_rm0d0qx2r32ln2f7_dpnrw40000gn/T/main-3d5e25.o -L/usr/local/lib -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.0.0/lib/darwin/libclang_rt.osx.a

Solution

  • > brew install mysql-connector-c
    > clang mysql_connect_test.c -l mysqlclient -o mysql_test
    > ./mysql_test
    MySQL client version: 6.1.11
    

    same code in mysql_connect_test.c, everything is ok.

    > ls /usr/local/Cellar/mysql-connector-c/6.1.11/lib
    libmysqlclient.18.dylib libmysqlclient.a        libmysqlclient.dylib
    

    Your code just need link to libmysqlclient.

    And in your post:

    gcc src/main.c \
        -Wall \
        -Ilib/mysql-connector-c-6.1.11-macos10.12-x86_64/include \
        -o bin/test
    

    It is wrong, because you just pass library search path to gcc, but also need pass which library you should link, for example, -lmysqlclient.

    gcc src/main.c \
        -Wall \
        -Ilib/mysql-connector-c-6.1.11-macos10.12-x86_64/include \
        -Llib/mysql-connector-c-6.1.11-macos10.12-x86_64/mysql-connector-c-6.1.11-src/libmysql \
        -lmysql \
        -o bin/test
    

    wrong name of link library, and mysql-connector-c-6.1.11-macos10.12-x86_64 just contains source code? If yes, you need compile mysql-connector-c first.