【问题标题】:How can I compile mysql-connector-c for macOS 10.13.x using C?如何使用 C 为 macOS 10.13.x 编译 mysql-connector-c?
【发布时间】:2018-06-14 19:44:37
【问题描述】:

概要

我有一个小的 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

架构 x86_64 的未定义符号: “_mysql_get_client_info”,引用自: _main 在 main-59d4fb.o ld:未找到架构 x86_64 的符号 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

然后我尝试将 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: 找不到 -lmysql 的库 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

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

另一种尝试

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

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

结论

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


任何想看到-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

【问题讨论】:

    标签: c c command-line-interface macos-high-sierra mysql-connector-c


    【解决方案1】:
    > 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
    

    您的代码只需要链接到 libmysqlclient

    在你的帖子中:

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

    错了,因为你只是将库搜索路径传递给gcc,还需要传递你应该链接哪个库,例如-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
    

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

    【讨论】:

    • 在这里感谢您的帮助,我想我在阅读本文时意识到我的错误 - 我在之前的尝试中确实使用了 -lmysqlclient,但由于无法链接到库而失败由于没有编译它,它不存在。我之前确实编译过它,但我没有使用正确的名称mysqlclient,因此我删除了它并让自己感到困惑。看着它,我只需要从here 下载源代码,编译你说的然后我的错误就是-lmysql 现在我用-lmysqlclient 工作了。
    【解决方案2】:

    我给卡特琳娜的两分钱:

    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>
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 2013-08-10
      • 2021-03-17
      • 1970-01-01
      相关资源
      最近更新 更多