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
Patricia ArquetteOriginal
2024-11-25 10:50:11347browse

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}

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
  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}
  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!

Statement:
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