1

I have a simple C++ program that does nothing:

int main() { return 0; }

I am trying to build this program completely statically using the following command:

g++ -o c c.cc -static

Everything works fine. However, when I try to link OpenMP, which is not actually a library, using the -fopenmp flag in static mode like this:

g++ -o c c.cc -fopenmp -static

The compiler gives an error:

/usr/bin/ld: cannot find -lgomp: No such file or directory
collect2: error: ld returned 1 exit status

This issue occurs with clang++ as well. The same happens with gcc and g++. However, other libraries that have static versions, such as curl, link correctly in static mode.

(I am on Arch Linux)

5
  • 1
    Clang has -static-openmp. Commented Jan 13 at 15:52
  • 2
    The gcc implementation of OpenMP, like many others comes as a compiler pass and a runtime library. For an OpenMP program to work you always need to link the OpenMP runtime, which is implied by -fopenmp. For static linking, you need to make sure that you have the static libgomp available on your system. Commented Jan 14 at 7:10
  • 1
    If you're deception really is all yours doing, then static linking is fine. However, of your then going to move on to "So now I can statically link the OpenMP into a shared library I want to distribute, so as to save the hassle of ensuring an OpenMP dynamic library is present", you are trading a very dangerous path. Having more than one OpenMP library (even for the same compiler) can break things. And if you're shipping a shared library you have no knowledge of the environment in which it will execute, which may already be dynamically linking an OpenMP library... Commented Jan 14 at 15:27
  • 1
    paleonix -static-openmp, It has not been added yet and is in version 20 (llvm) Commented Jan 15 at 11:34
  • Ah, sorry. BTW to notify people use @name instead of linking their profile. The name should autocomplete on tab. I only saw your comment due to following the post. Commented Jan 15 at 12:48

1 Answer 1

2

Your system is missing the static version of the GNU OMP runtime, libgomp.a. Archlinux does not package it. Unlike most distros, Archlinux doesn't do development packages as distinct from runtime packages. The shared library version, only, is installed by package gcc-libs. To get the static version built on Archlinux you would need to build GCC from source, which may be too high a price. You've nothing to lose by seeing if some other distro's build for the same architecure will work. You could try downloading, say, the GCC 13 Ubuntu x86_64 development package that contains it such as:

libgcc-13-dev_13.3.0-6ubuntu2~24.04_amd64.deb 

from:

http://security.ubuntu.com/ubuntu/pool/main/g/gcc-13/

into your downloads directory. Or similarly for whatever GCC version you've got. Then extract it (it is actually an ar.tar.zst archive):

ar -x libgcc-13-dev_13.3.0-6ubuntu2~24.04_amd64.deb
tar -xf data.tar.zst

which will extract ./usr in the current directory. You will find lbgomp.a at:

./usr/lib/gcc/x86_64-linux-gnu/13/libgomp.a

Copy that libgomp.a to your build directory, then see how you fare by linking:

$ g++ -o c c.cc -fopenmp -static -L. -lgomp

Or I should say, see how you fare when you actually put some omp code in the program.

Don't have archlinux; haven't tried it.

Sign up to request clarification or add additional context in comments.

1 Comment

It was interesting. I tested this in Ubuntu as well, and it builds correctly in the -static mode. However, one downside is that the omp_set_dynamic mode gets disabled, and automatic load balancing does not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.