Calling a shared library in C++ results in ld.lld: error: undefined symbol:

1,512 views
Skip to first unread message

Eat Sponsors

unread,
Jan 6, 2023, 9:02:36 AM1/6/23
to chromi...@chromium.org
Can anyone suggest why I am getting undefined symbol? thanks!

Using C++, my goal is to call a function from my own shared library from within Chromium.

When I compile, I get ld.lld: error: undefined symbol: my_class::print_a_dot()




    ubuntu:~/chromium/src> autoninja -C out/Default chrome
    ninja: Entering directory `out/Default'
    [16888/17733] SOLINK ./libcontent.so
    FAILED: libcontent.so libcontent.so.TOC
    python3 "../../build/toolchain/gcc_solink_wrapper.py" --readelf="../../third_party/llvm-build/Release+Asserts/bin/llvm-readelf" --nm="../../third_party/llvm-build/Release+Asserts/bin/llvm-nm"  --sofile="./libcontent.so" --tocfile="./libcontent.so.TOC" --output="./libcontent.so" -- ../../third_party/llvm-build/Release+Asserts/bin/clang++ -shared -Wl,-soname="libcontent.so" -Werror -fuse-ld=lld -Wl,--fatal-warnings -Wl,--build-id -fPIC -Wl,-z,noexecstack -Wl,-z,relro -Wl,--color-diagnostics -Wl,--undefined-version -Wl,--no-call-graph-profile-sort -m64 -no-canonical-prefixes -Wl,--gdb-index -rdynamic -Wl,-z,defs -Wl,--as-needed -nostdlib++ --sysroot=../../build/linux/debian_bullseye_amd64-sysroot -Wl,-rpath=\$ORIGIN -o "./libcontent.so" @"./libcontent.so.rsp"
    ld.lld: error: undefined symbol: my_class::print_a_dot()
    >>> referenced by delegated_frame_host.cc:457 (../../content/browser/renderer_host/delegated_frame_host.cc:457)
    >>>               obj/content/browser/browser/delegated_frame_host.o:(content::DelegatedFrameHost::DidCopyStaleContent(std::Cr::unique_ptr<viz::CopyOutputResult, std::Cr::default_delete<viz::CopyOutputResult>>))
    clang++: error: linker command failed with exit code 1 (use -v to see invocation)
    [16913/17733] CXX irt_x64/obj/ipc/mojom/ipc.mojom.o
    ninja: build stopped: subcommand failed.
   :ubuntu:~/chromium/src>


For my shared library, outside Chroimium I created a C++ source file my_class.cpp


    ubuntu:~/learn> cat my_class.cpp
    #include <stdio.h>
    #include <iostream>
    #include <memory>
    #include <stdint.h>
    #include "my_class.h"
   
   
    extern void my_class::print_a_dot()
    {
        std::cout << ".";
    }

For my shared library, outside Chromium I created a C++ header file my_class.h

    :ubuntu:~/learn> cat my_class.h
    #ifndef MY_CLASS_H    // To make sure you don't declare the function more than once by including the header multiple times.
    #define MY_CLASS_H
   
    class my_class {
   
      public:
          static int some_number;
          static bool initialised;
          void print_a_dot();
          int init_encoder();
      };
   
   
    #endif

For my shared library, outside Chromium I created a CMakeLists.txt

    :ubuntu:~/learn> cat CMakeLists.txt
    cmake_minimum_required(VERSION 3.15)
   
    project(MyProject)
   
    add_library(myexample SHARED my_class.cpp)
   
    :ubuntu:~/learn>


In the Chromium third_party directory named "dude4" I created a directory so Chromium can access my shared library:

    third_party % find dude4
    dude4
    dude4/BUILD.gn
    dude4/include
    dude4/include/my_class.h
    third_party %

The BUILD.gn contains:


    config("my_class_import") {
      include_dirs = ["include"]
      libs = ["/home/ubuntu/learn/build/libmyexample.so"]
    }
   
    group("my_class") {
      public_configs = [":my_class_import"]
    }

I want to call my shared library from within Chromium, so I add the following lines at the bottom of the header lines in delegated_frame_host.cc

    #include "third_party/dude4/include/my_class.h"
   
    //using namespace N;
    bool my_class::initialised = false;
    int my_class::some_number = 999;

And also in delegated_frame_host.cc I add some code to call my shared library:

    void DelegatedFrameHost::DidCopyStaleContent(
        std::unique_ptr<viz::CopyOutputResult> result) {
      // host may have become visible by the time the request to capture surface is
      // completed.
   
        my_class mc;
        mc.initialised = false;
        std::cout << "my_class::some_number: ";
        std::cout << my_class::some_number << std::endl;
        my_class::some_number = 888;
        std::cout << "mc.some_number: ";
        std::cout << mc.some_number << std::endl;
        my_class nc;
        std::cout << "nc.some_number: ";
        std::cout << nc.some_number << std::endl;
   
        if (mc.initialised == false) {
            std::cout << "initialising encoder" << std::endl;
            mc.initialised = true;
        }
        mc.print_a_dot();
    (code of this function continues.......)











K. Moon

unread,
Jan 6, 2023, 1:24:03 PM1/6/23
to eatups...@gmail.com, chromi...@chromium.org

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAJfzVqTBBPJi0c%3DkyHdPYxTqkZh6K924%3DNXKVbiyS2c90HruLg%40mail.gmail.com.

Eat Sponsors

unread,
Jan 7, 2023, 11:54:17 AM1/7/23
to Chromium-dev, km...@chromium.org, chromi...@chromium.org, Eat Sponsors
Apologies ..... my posts to this list did not appear for a very long time (maybe a day or more, not sure?) and eventually I tried sending again, which also did not appear, then I found I could post to gn-dev

So apologies to the group for the reposts.

Reply all
Reply to author
Forward
0 new messages