Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Build static library but symbols not found for architecture x86_64 in iOS



  • Hi,
    I've had trouble using a static library built by qt c++. The error shown in Xcode is:
    " Undefined symbols for architecture x86_64:
    "_add", referenced from:
    -[ViewController viewDidLoad] in ViewController.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation) "

    My config in Qt:
    ![qt config](Screen Shot 2022-01-10 at 18.08.19.png image url)

    My code C++:
    ![file cpp](Screen Shot 2022-01-10 at 18.10.00.png image url)

    ![file header](Screen Shot 2022-01-10 at 18.10.18.png image url)

    My error when add the static library in xcode:
    Screen Shot 2022-01-10 at 09.11.40.png

    Thanks in advance for any help.


  • Lifetime Qt Champion

    This post is deleted!

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you share the content of your CMakeLists.txt file ?



  • Hi, linking with a static c++ library should work fine on the iPhone, but since Objective-C was invented before C++ it has no knowledge of c++ name mangling.

    Either you rewrite your static library in plain C, or a better solution, change all of your Objective-c with extension .m to instead have the extension .mm. Edit: forgot:
    except main.m, that file keeps the .m extension
    . Now the name mangling should work.

    Also check that you have a -L and -l linking directive for Xcode, for example you can put it in the "Other Linker Flags" in Xcode's Linking setting for your project, eg:

    -L /Users/vietanh/build-Hello-Qt_6_2_2_for_iOS_Simulator/Debug-iphonesimulator
    -l Hello
    

    and verify that you have a static library libHello.a in that folder: /Users/vietanh/build-Hello-Qt_6_2_2_for_iOS_Simulator/Debug-iphonesimulator



  • @SGaist Hi, There is my CMakeLists.txt file:
    ![alt text](Screen Shot 2022-01-11 at 07.57.42.png image url)



  • @hskoglund said in Build static library but symbols not found for architecture x86_64 in iOS:

    Hi, linking with a static c++ library should work fine on the iPhone, but since Objective-C was invented before C++ it has no knowledge of c++ name mangling.

    Either you rewrite your static library in plain C, or a better solution, change all of your Objective-c with extension .m to instead have the extension .mm. Edit: forgot:
    except main.m, that file keeps the .m extension
    . Now the name mangling should work.

    Also check that you have a -L and -l linking directive for Xcode, for example you can put it in the "Other Linker Flags" in Xcode's Linking setting for your project, eg:

    -L /Users/vietanh/build-Hello-Qt_6_2_2_for_iOS_Simulator/Debug-iphonesimulator
    -l Hello
    

    and verify that you have a static library libHello.a in that folder: /Users/vietanh/build-Hello-Qt_6_2_2_for_iOS_Simulator/Debug-iphonesimulator

    Hi, thanks for your support. I have tried it but have the same problem.
    Screen Shot 2022-01-11 at 08.24.40.png



  • Hi, check that all Objective-C files using your static library have an extension .mm (not .m) (If ViewController.mm is the only file calling into your static library then it's ok now.)

    Check that you have a valid libHello.a file in your directory /Users/vietanh/build-Hello-Qt_6_2_2_for_iOS_Simulator/Debug-iphonesimulator, start a Terminal and type:

    ar -t /Users/vietanh/build-Hello-Qt_6_2_2_for_iOS_Simulator/Debug-iphonesimulator/libHello.a
    

    the output should be:
    __.SYMDEF SORTED
    Hello.o

    In Xcode, check that you have the the linking directive:

     -L /Users/vietanh/build-Hello-Qt_6_2_2_for_iOS_Simulator/Debug-iphonesimulator -l Hello
    

    (note: -l libHello or -l libHello.a do not work)


Log in to reply