如何使用 C 为 macOS 10.13.x 编译 mysql-connector-c?

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

剧情简介

我有一个小的 c 程序,它是一个标准的 "hello world" 应用程序来测试我可以相应地访问 mysql 连接驱动程序:

#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;
}

当我使用以下代码编译它时;我得到一个错误:

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)

然后我尝试 link mysql 使用以下内容:

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)

值得注意的是我使用的是 而不是 c++,我 不是 使用 XCode 只是 Vim 和 gcc如上所示。

另一种尝试

然后我决定也许我从 MySQL This particular link 编译源代码时做错了,所以我删除了我的应用程序中的 lib 目录并通过自制软件安装它们:

我尝试了 brew install mysql-connector-c 尽管相应地更改了 -I -L -l 新路径 /usr/local/Cellar/... 我也收到了与上述相同的错误并且出现了完全相同的问题。

结论

我在任何已编译的 mysql 库中都找不到任何 .c 文件,我确实在我编译并尝试 link 的源代码中找到了一个 libmysql那个也失败了。我个人认为这是一个 linking 问题,我只是不知道是什么或如何解决的,如果有人可以帮助我解决这个问题,我将不胜感激。


任何想看-v回复的人请看下面:

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
> brew install mysql-connector-c
> clang mysql_connect_test.c -l mysqlclient -o mysql_test
> ./mysql_test
MySQL client version: 6.1.11

同样的代码在mysql_connect_test.c,一切正常

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

你的代码只需要 link 到 libmysqlclient.

在你的 post 中:

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

这是错误的,因为你只是将库搜索路径传递给gcc,还需要传递你应该link的库link,例如-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

link 库的名称错误,mysql-connector-c-6.1.11-macos10.12-x86_64 只包含源代码?如果是,需要先编译mysql-connector-c

我给 Catalina 的两分钱:

Use of external C++ headers in Objective-C

有效,使用 mysql lib 版本 21。

无需使用 brew 或类似工具..

注:

因为该库未签名。只是在 Entitlemens:

(在XML):..

<dict>
    <key>com.apple.security.cs.disable-library-validation</key>
    <true/>
</dict>
...