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