【问题标题】:linking to a .dylib in vs code在 vs 代码中链接到 .dylib
【发布时间】:2020-12-04 16:18:31
【问题描述】:

我试图在我的沙盒文件夹中编译时链接我的 .dylib 文件,但从编译器收到 unknown / unsupported file 警告,然后是我的类中的未定义符号。该库似乎是正确的,只有一个包装的 printf 并显示为: Mach-O 64-bit dynamically linked shared library x86_64

对构建和链接有任何帮助吗?谢谢。

{
    "version": "2.0.0",
    "tasks": [
        { 

        { 
            "label": "build",
            "type": "shell",
            "command": "g++ --verbose  -m64 -dynamiclib -fPIC -o ${workspaceFolder:Engine}/bin/libfoo.dylib ${workspaceFolder:Engine}/src/test.cpp"
        },


            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [ 
                "--verbose",
                "${workspaceFolder:Sandbox}/src/testinglib.cpp",
                "${file}",
                "-I", 
                 "${workspaceFolder:Engine}/src",
                "-L", 
                "${workspaceFolder:Engine}/bin",
                 "-o",
                "${workspaceFolder:Sandbox}/bin/testinglib",      
                 ]
        }
    ]
}
ld: warning: ignoring file /Development/Engine/.vscode/tasks.json, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x7B 0x0A 0x20 0x20 0x20 0x20 0x2F 0x2F 0x20 0x53 0x65 0x65 0x20 0x68 0x74 0x74 )
Undefined symbols for architecture x86_64:
  "Engine::Print()", referenced from:
      _main in libtesting-77a284.o
ld: symbol(s) not found for architecture x86_64

【问题讨论】:

  • lib 是您的 dylib 的不幸名称。尝试调用它libfoo.dylib,然后在你的 json 文件中使用-l foo。我还认为您可能需要将L 全部放在一行中。
  • 感谢您的回复。当调用它libfoo 时,它仍然抱怨找不到foo,我还注意到如果我输入"L ${workspaceFolder:Engine}/bin" 它抱怨找不到-L 的目录。在没有 -l 的情况下将 -L 参数保留在 2 行似乎更进一步。现在它尝试 link with file built for unknown-unsupported file format ,也许我错误地编译了库,因为下一条消息说它无法解析我在 cpp 的 main 中调用的符号。有什么资源可以学习如何正确地做到这一点?
  • link with file build for unknown-unsupported file format 听起来有点不祥。 file 为该文件报告什么?你也可以试试lipo -info
  • 它看起来是正确的 libfoo.dylib: Mach-O 64-bit 动态链接共享库 x86_64 从 lipo 我们得到 Non-fat file: /Users/.. ./libfoo.dylib 是架构:x86_64
  • 好的,您能完整发布相关的错误消息吗? (将它们添加到您的问题中)。

标签: c++ macos visual-studio-code


【解决方案1】:

修复了解决问题的参数格式。还添加了dependsOn,它首先创建库,然后构建依赖对象,最后在沙箱文件夹中运行可执行文件。

    "version": "2.0.0",
    "tasks": [
        { // creates dynamiclib
            "label": "build_lib",
            "type": "shell",
            "command": "g++",
            "args":
             [ "-dynamiclib",
                "-fPIC",// osx doesn't seem to need this but ok
                "-o",
                "${workspaceFolder:Engine}/bin/libfoo.dylib",
                "${workspaceFolder:Engine}/src/test.cpp",
            ],
            "group": "build"
        },

        { 
            "label": "build_engine",
            "type": "shell",
            "command": "g++",
            "args": [ 
                "--verbose",
                "${workspaceFolder:Sandbox}/src/libtesting.cpp",
                "-I", // include headers path for compiler
                 "${workspaceFolder:Engine}/src",
                "-L", // Library directory 
                "${workspaceFolder:Engine}/bin",
                "-l", // point to created libfoo library foo
                "foo",
                 "-o",
                "${workspaceFolder:Sandbox}/bin/testingout",      
                 ],
            "group":"build",
            "dependsOn":["build_lib"]
        },

        {
            "label": "run_sandbox",
            "type": "shell",
            "command": "${workspaceFolder:Sandbox}/bin/testingout",
            "group": "build",
            "dependsOn":["build_engine"]
        }
    ]
}

【讨论】:

    猜你喜欢
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 2010-09-06
    相关资源
    最近更新 更多