Search code examples
c++g++linker-errorsstatic-linkingvulkan

Why can't I link against shaderc?


I have a vulkan project that is trying to use shaderc.

The library was installed under the following path (linux system):

./libraries/shaderc/build/libshaderc

Doing ls in this directory gives:

CMakeFiles                         shaderc_combined_shaderc_test
cmake_install.cmake                shaderc_c_smoke_test
CTestTestfile.cmake                shaderc_shaderc_cpp_test
libshaderc.a                       shaderc_shaderc_private_test
libshaderc_combined.a              shaderc_shaderc_test
libshaderc_shared.so               shaderc_shared_shaderc_cpp_test
libshaderc_shared.so.1             shaderc_shared_shaderc_private_test
shaderc_combined.ar                shaderc_shared_shaderc_test
shaderc_combined_shaderc_cpp_test

The documentation provided with the library explicitly states:

  1. If the external project does not use CMake, then the external project can instead directly use the generated libraries. shaderc/libshaderc/include should be added to the include path, and build/libshaderc/libshaderc_combined.a should be linked. Note that on some platforms -lpthread should also be specified.

I attempt to link the project as follows using make:

g++ -o "../build/VulkanEngine" obj/Debug/DebugCallback.o obj/Debug/Device.o obj/Debug/log.o obj/Debug/ImageViews.o obj/Debug/Instance.o obj/Debug/Pipeline.o obj/Debug/RenderPass.o obj/Debug/Surface.o obj/Debug/SwapChain.o obj/Debug/VkExtensionsStubs.o obj/Debug/GLFW_tools.o obj/Debug/main.o    -L../libraries/glfw-3.2.1/bin -L../libraries/glm/bin/glm -L../libraries/vulkansdk-linux/1.1.97.0/x86_64/lib -L../libraries/shaderc/build/libshaderc -lstdc++fs -lglfw -lglm_static-lvulkan -llibshaderc_combined.a

In particular notice the argument -L../libraries/shaderc/build/libshaderc: and the argument -llibshaderc_combined.a

As you can see the specified linking path matches the installation path (libraries/ contains all the third party libraries my project uses).

However the linker complains:

/usr/bin/ld: cannot find -llibshaderc_combined.a
collect2: error: ld returned 1 exit status
make[1]: *** [VulkanEngine.make:119: ../build/VulkanEngine] Error 1
make: *** [Makefile:30: VulkanEngine] Error 2

So it seems I messed up the path somehow, but I don;t know how.


Solution

  • The -lname linkage option directs the linker to search in specified (-Ldir) and default library search directories for either of the files libname.so (shared library) or libname.a (static library). If it finds either of them them it searches no more directories and inputs that file to the linkage. If it finds both of them in the same search directory then it will prefer libname.so.

    So change:

    -llibshaderc_combined.a
    

    to:

    -lshaderc_combined
    

    You must also be sure that -lshaderc_combined appears in the linkage sequence before any other library that it depends on, and after any object file or library that depends upon it, or the linkage is liable to fail with undefined reference errors.