Home > Backend Development > Golang > How to Statically Link a Go CGO Executable with Oracle Libraries on Linux?

How to Statically Link a Go CGO Executable with Oracle Libraries on Linux?

Patricia Arquette
Release: 2024-11-25 10:50:11
Original
282 people have browsed it

How to Statically Link a Go CGO Executable with Oracle Libraries on Linux?

Linking for Static Execution of Go CGO Executable with Oracle Libraries on Linux

Problem Statement:

Building a basic Go snippet interfacing with an Oracle database fails when compiling statically using the command:

CGO_ENABLED=1 go build -work -x -ldflags "-v -linkmode external -extldflags -static"  ${MAIN_SRC}
Copy after login

The compilation error indicates that the Oracle library -lclntsh cannot be found. Despite attempts to set environment variables and install additional files from the Oracle database package, the static compilation still fails.

Solution:

  1. Generate the Static Library:

    Oracle typically does not ship with the static library libclntst.a. To generate it, run the following command:

    $ORACLE_HOME/bin/genclntst
    Copy after login
  2. Link the Application:

    Link your application with the generated static library:

    CGO_ENABLED=1 go build -work -x -ldflags "-v -linkmode external -extldflags -static -L/usr/lib/oracle/12.1/client64/lib -lclntsh -lclntst"  ${MAIN_SRC}
    Copy after login
  3. Handle Missing Symbols (Optional):

    If there are still missing symbols, use the nm tool to identify them. Then, add additional static libraries as needed to resolve the dependencies.

Additional Notes:

  • Using linker utility from Oracle may be helpful for analyzing dependencies.
  • Depending on the Oracle version and compiler, it may be necessary to link with additional ICC runtime static libraries.
  • Static linking requires manually resolving all dependencies, which can be complex.

The above is the detailed content of How to Statically Link a Go CGO Executable with Oracle Libraries on Linux?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template