Search code examples
linuxmakefileg++linker-errorsanura

`ld` complains of no such file or directory, when it exists by exact name


I have a rather old-school Makefile which calls g++ to compile my program on Ubuntu¹. When I run make, it prints out the following error after some compilation happens.

/usr/bin/ld: cannot find -l/usr/lib/x86_64-linux-gnu/libX11.so: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [Makefile:188: anura] Error 1

However, a file by that exact name exists.

$ ls -l /usr/lib/x86_64-linux-gnu/libX11.so
lrwxrwxrwx 1 root root 15 Apr  3  2022 /usr/lib/x86_64-linux-gnu/libX11.so -> libX11.so.6.4.0

I can cat it, and I get roughly the expected contents.

I've checked the command being run, and it seems to be constructed appropriately. I haven't done anything stupid like -l-l/usr/lib... or something.

g++ \
      -O2 -Wall -Werror -Wno-literal-suffix -Wno-sign-compare -I/usr/include/SDL2 -D_REENTRANT -std=c++0x -g -fno-inline-functions -fthreadsafe-statics -Wno-narrowing -Wno-reorder -Wno-unused -Wno-unknown-pragmas -Wno-overloaded-virtual -DBOOST_BIND_GLOBAL_PLACEHOLDERS -Wno-deprecated-declarations -I./vcpkg_installed/x64-linux/include -DUSE_SVG -DUSE_IMGUI -rdynamic -L./vcpkg_installed/x64-linux/lib  -DIMGUI_INCLUDE_IMGUI_USER_INL -MMD -MP \
      ./build/kre/AttributeSet.o ./build/kre/AttributeSetOGL.o ./build/kre/Blend.o ./build/kre/BlendModeScope.o ./build/kre/BlendOGL.o ./build/kre/Blittable.o ./build/kre/CameraObject.o ./build/kre/Canvas.o ./build/kre/CanvasOGL.o ./build/kre/ClipScope.o ./build/kre/ClipScopeOGL.o ./build/kre/Color.o ./build/kre/ColorScope.o ./build/kre/Cursor.o ./build/kre/Depth.o ./build/kre/DisplayDevice.o ./build/kre/DisplayDeviceOGL.o ./build/kre/DisplayDeviceOGLFixed.o ./build/kre/DisplayDeviceSDL.o ./build/kre/EffectsOGL.o ./build/kre/FboOGL.o ./build/kre/Font.o ./build/kre/FontDriver.o ./build/kre/FontFreetype.o ./build/kre/FontSDL.o ./build/kre/FontSTB.o ./build/kre/Frustum.o ./build/kre/Gradients.o ./build/kre/imgui_impl_sdl_gl3.o ./build/kre/LightObject.o ./build/kre/ModelMatrixScope.o ./build/kre/ParticleSystemAffectors.o ./build/kre/ParticleSystem.o ./build/kre/ParticleSystemEmitters.o ./build/kre/ParticleSystemParameters.o ./build/kre/ParticleSystemUI.o ./build/kre/Renderable.o ./build/kre/RenderManager.o ./build/kre/RenderQueue.o ./build/kre/RenderTarget.o ./build/kre/SceneGraph.o ./build/kre/SceneNode.o ./build/kre/SceneObject.o ./build/kre/SceneParameters.o ./build/kre/SceneTree.o ./build/kre/Scissor.o ./build/kre/ScissorOGL.o ./build/kre/Shaders.o ./build/kre/ShadersOGL.o ./build/kre/StencilScope.o ./build/kre/StencilScopeOGL.o ./build/kre/SurfaceBlur.o ./build/kre/Surface.o ./build/kre/SurfaceScale.o ./build/kre/SurfaceSDL.o ./build/kre/TexPack.o ./build/kre/Texture.o ./build/kre/TextureOGL.o ./build/kre/TextureSDL.o ./build/kre/TextureUtils.o ./build/kre/UniformBuffer.o ./build/kre/UniformBufferOGL.o ./build/kre/Util.o ./build/kre/VGraphCairo.o ./build/kre/VGraph.o ./build/kre/VGraphOGL.o ./build/kre/VGraphOGLFixed.o ./build/kre/WindowManager.o ./build/svg/easy_svg.o ./build/svg/svg_attribs.o ./build/svg/svg_container.o ./build/svg/svg_element.o ./build/svg/svg_fwd.o ./build/svg/svg_gradient.o ./build/svg/svg_length.o ./build/svg/svg_paint.o ./build/svg/svg_parse.o ./build/svg/svg_path_parse.o ./build/svg/svg_shapes.o ./build/svg/svg_style.o ./build/svg/svg_transform.o ./build/svg/svg_utils.o ./build/tiled/tiled.o ./build/tiled/tmx_reader.o ./build/hex/hex_helper.o ./build/hex/hex_loader.o ./build/hex/hex_map.o ./build/hex/hex_mask.o ./build/hex/hex_renderable.o ./build/hex/hex_tile.o ./build/hex/tile_rules.o ./build/xhtml/css_lexer.o ./build/xhtml/css_parser.o ./build/xhtml/css_properties.o ./build/xhtml/css_selector.o ./build/xhtml/css_styles.o ./build/xhtml/css_stylesheet.o ./build/xhtml/css_transition.o ./build/xhtml/event_listener.o ./build/xhtml/scrollable.o ./build/xhtml/solid_renderable.o ./build/xhtml/to_roman.o ./build/xhtml/url_handler.o ./build/xhtml/xhtml_absolute_box.o ./build/xhtml/xhtml_background_info.o ./build/xhtml/xhtml_block_box.o ./build/xhtml/xhtml_border_info.o ./build/xhtml/xhtml_box.o ./build/xhtml/xhtml_element.o ./build/xhtml/xhtml_inline_block_box.o ./build/xhtml/xhtml_inline_element_box.o ./build/xhtml/xhtml_layout_engine.o ./build/xhtml/xhtml_line_box.o ./build/xhtml/xhtml_listitem_box.o ./build/xhtml/xhtml_node.o ./build/xhtml/xhtml_parser.o ./build/xhtml/xhtml_render_ctx.o ./build/xhtml/xhtml_root_box.o ./build/xhtml/xhtml_script_interface.o ./build/xhtml/xhtml_style_tree.o ./build/xhtml/xhtml_text_box.o ./build/xhtml/xhtml_text_node.o ./build/xhtml/xslider.o ./build/achievements.o ./build/animation_creator.o ./build/animation_preview_widget.o ./build/animation_widget.o ./build/anura_shader.o ./build/asserts.o ./build/auto_update_window.o ./build/background.o ./build/background_task_pool.o ./build/bar_widget.o ./build/base64.o ./build/blur.o ./build/border_widget.o ./build/breakpad.o ./build/button.o ./build/cairo.o ./build/character_editor_dialog.o ./build/checkbox.o ./build/checksum.o ./build/clipboard.o ./build/code_editor_dialog.o ./build/code_editor_widget.o ./build/collision_utils.o ./build/color_picker.o ./build/ColorTransform.o ./build/compress.o ./build/controls.o ./build/controls_dialog.o ./build/current_generator.o ./build/custom_object_callable.o ./build/custom_object.o ./build/custom_object_dialog.o ./build/custom_object_functions.o ./build/custom_object_type.o ./build/db_client.o ./build/debug_console.o ./build/decimal.o ./build/dialog.o ./build/difficulty.o ./build/distortion.o ./build/drag_widget.o ./build/draw_primitive.o ./build/draw_scene.o ./build/draw_tile.o ./build/dropdown_widget.o ./build/editor.o ./build/editor_dialogs.o ./build/editor_formula_functions.o ./build/editor_layers_dialog.o ./build/editor_level_properties_dialog.o ./build/editor_module_properties_dialog.o ./build/editor_stats_dialog.o ./build/editor_variable_info.o ./build/entity.o ./build/external_text_editor.o ./build/ffl_dom.o ./build/ffl_lib.o ./build/ffl_weak_ptr.o ./build/file_chooser_dialog.o ./build/filesystem-android.o ./build/filesystem.o ./build/formula_callable.o ./build/formula_callable_definition.o ./build/formula_callable_visitor.o ./build/formula_constants.o ./build/formula.o ./build/formula_function.o ./build/formula_function_registry.o ./build/formula_garbage_collector.o ./build/formula_interface.o ./build/formula_internal.o ./build/formula_object.o ./build/formula_profiler.o ./build/formula_test.o ./build/formula_tokenizer.o ./build/formula_variable_storage.o ./build/formula_visualize_widget.o ./build/formula_vm.o ./build/frame.o ./build/framed_gui_element.o ./build/ft_iface.o ./build/game_registry.o ./build/geometry_callable.o ./build/globals.o ./build/graphical_font.o ./build/graphical_font_label.o ./build/grid_widget.o ./build/group_property_editor_dialog.o ./build/gui_section.o ./build/http_client.o ./build/http_server.o ./build/i18n.o ./build/image_widget.o ./build/input.o ./build/intrusive_ptr.o ./build/isochunk.o ./build/isoworld.o ./build/joystick.o ./build/json_parser.o ./build/json_tokenizer.o ./build/key_button.o ./build/label.o ./build/language_dialog.o ./build/LayerBlitInfo.o ./build/layout_widget.o ./build/level.o ./build/level_logic.o ./build/level_object.o ./build/level_runner.o ./build/level_solid_map.o ./build/light.o ./build/loading_screen.o ./build/load_level.o ./build/load_level_nothread.o ./build/logger.o ./build/main.o ./build/md5.o ./build/message_dialog.o ./build/module.o ./build/module_web_server.o ./build/multiplayer.o ./build/multiplayer_server.o ./build/multi_tile_pattern.o ./build/normal_map.o ./build/object_events.o ./build/particle_system.o ./build/particle_system_proxy.o ./build/ParticleSystemWidget.o ./build/pathfinding.o ./build/pause_game_dialog.o ./build/playable_custom_object.o ./build/player_info.o ./build/poly_line_widget.o ./build/poly_map.o ./build/preferences.o ./build/preprocessor.o ./build/preview_tileset_widget.o ./build/progress_bar.o ./build/property_editor_dialog.o ./build/random.o ./build/rectangle_rotator.o ./build/rect_renderable.o ./build/rich_text_label.o ./build/SceneNodeCallable.o ./build/SceneObjectCallable.o ./build/screen_handling.o ./build/scrollable_widget.o ./build/scrollbar_widget.o ./build/segment_editor_dialog.o ./build/settings_dialog.o ./build/shared_memory_pipe.o ./build/simplex_noise.o ./build/simplex_noise_tests.o ./build/skybox.o ./build/slider.o ./build/solid_map.o ./build/sound.o ./build/speech_dialog.o ./build/StackWalker.o ./build/stats.o ./build/stats_server.o ./build/stats_server_main.o ./build/stats_web_server.o ./build/string_utils.o ./build/surface_cache.o ./build/surface_palette.o ./build/surface_utils.o ./build/sys.o ./build/tbs_ai_player.o ./build/tbs_bot.o ./build/tbs_client.o ./build/tbs_functions.o ./build/tbs_game.o ./build/tbs_internal_client.o ./build/tbs_internal_server.o ./build/tbs_ipc_client.o ./build/tbs_matchmaking_server.o ./build/tbs_relay_server.o ./build/tbs_server_base.o ./build/tbs_server.o ./build/tbs_web_server.o ./build/text_editor_widget.o ./build/TextureObject.o ./build/theme_imgui.o ./build/thread.o ./build/tile_map.o ./build/tileset_editor_dialog.o ./build/tooltip.o ./build/translate.o ./build/tree_view_widget.o ./build/unit_test.o ./build/user_voxel_object.o ./build/utility_object_compiler.o ./build/utility_query.o ./build/utility_render_level.o ./build/utils.o ./build/uuid.o ./build/variant_callable.o ./build/variant.o ./build/variant_type_check.o ./build/variant_type.o ./build/variant_utils.o ./build/video_selections.o ./build/VoronoiDiagramGenerator.o ./build/voxel_animation.o ./build/voxel_editor.o ./build/voxel_model.o ./build/voxel_object.o ./build/voxel_object_functions.o ./build/voxel_object_type.o ./build/water.o ./build/water_particle_system.o ./build/weather_particle_system.o ./build/widget.o ./build/widget_editor.o ./build/widget_factory.o ./build/widget_settings_dialog.o ./build/wml_formula_callable.o imgui/imgui.o   imgui/imgui_draw.o   imgui/imgui_widgets.o -o anura \
      -lX11 -lGL -D_REENTRANT -pthread -I/home/ddr/Projects/Anura/vcpkg_installed/x64-linux/lib/pkgconfig/../../include -I/home/ddr/Projects/Anura/vcpkg_installed/x64-linux/lib/pkgconfig/../../include/SDL2 -I/home/ddr/Projects/Anura/vcpkg_installed/x64-linux/lib/pkgconfig/../../include/cairo -I/home/ddr/Projects/Anura/vcpkg_installed/x64-linux/lib/pkgconfig/../../include/lzo -I/home/ddr/Projects/Anura/vcpkg_installed/x64-linux/lib/pkgconfig/../../include -I/home/ddr/Projects/Anura/vcpkg_installed/x64-linux/lib/pkgconfig/../../include/libpng16 -I/home/ddr/Projects/Anura/vcpkg_installed/x64-linux/lib/pkgconfig/../../include -I/home/ddr/Projects/Anura/vcpkg_installed/x64-linux/lib/pkgconfig/../../include/pixman-1 -L/home/ddr/Projects/Anura/vcpkg_installed/x64-linux/lib/pkgconfig/../../lib -lX11 -lGLEW -lGLU -lSDL2_image -lSDL2_ttf -pthread -lSDL2 -lrt -ldbus-1 -l/usr/lib/x86_64-linux-gnu/libXxf86vm.so -l/usr/lib/x86_64-linux-gnu/libXss.so -l/usr/lib/x86_64-linux-gnu/libXrandr.so -l/usr/lib/x86_64-linux-gnu/libXfixes.so -l/usr/lib/x86_64-linux-gnu/libXi.so -l/usr/lib/x86_64-linux-gnu/libXinerama.so -l/usr/lib/x86_64-linux-gnu/libXcursor.so -l/usr/lib/x86_64-linux-gnu/libXext.so -l/usr/lib/x86_64-linux-gnu/libX11.so -lm -Wl,--no-undefined -pthread -lSDL2 -lcairo -lm -ldl -llzo2 -lfontconfig -pthread -lexpat -lm -lfreetype -lbz2 -lpng16 -lm -lz -lbrotlidec-static -lbrotlicommon-static -lpixman-1 -lm -pthread -logg -lvorbis -lvorbisfile -lrt -lstdc++ -lm -L/home/ddr/Projects/Anura/vcpkg_installed/x64-linux/lib/pkgconfig/../../lib -lcairo -lm -ldl -llzo2 -lfontconfig -pthread -lexpat -lm -lfreetype -lbz2 -lpng16 -lm -lz -lbrotlidec-static -lbrotlicommon-static -lpixman-1 -lm -pthread -lboost_regex -lboost_system -lboost_filesystem -lboost_locale -licui18n -licuuc -licudata -lpthread -fthreadsafe-statics

What do I need to do to fix this, or diagnose it further?


¹: Technically KDE Neon, which is based on Ubuntu.


Solution

  • a file by that exact name exists.

    But that's not what the linker is looking for.

    If you want to specify a link library via a path to its file, then just do so. Do not use -l in this case. This is rarely done, however, especially for system libraries.

    Traditionally, the argument to an -l option should be the library name, not its file name or path. For example, -lX11. That relies on the convention that a library's filename is constructed from its library name by prepending lib and appending .a or some other system-specific extension (.so, .dylib, etc).

    Alternatively, with recent versions of the GNU linker, you can prepend a colon (:) to the library's full basename to cause the linker to search the library search path for an object with that basename. For example, -l:libX11.so. This also is rarely used.

    If the library wanted is not in the default library search path, then conventionally, one would use a -L option to add its directory, rather than specifying the library by its full path. For example, -L/usr/lib/x86_64-linux-gnu -lX11. However, I believe that /usr/lib/x86_64-linux-gnu is in the default library search path on Ubuntu, so you should not need to do that for libX11.

    Note also that the order in which objects and libraries are specified on the command line is significant, with the result that occasionally it makes sense to specify the same library more than once. But that's unusual. One can ordinarily choose an object order that works without duplication. And there is no reason at all to specify the same library via arguments having different form than one another.