C/C++ module for stddef.h?

Hello, I’m trying to compile some code I need as part of my migration to Discovery, and I ran into the following issue when trying to build a makefile:

[sagendor@discovery build]$ make                                                                                                                                                                                                       
Scanning dependencies of target NanoShaper                                                                                                                                                                                                   
[  6%] Building CXX object CMakeFiles/NanoShaper.dir/src/sturm/sturm.cpp.o                                                                                                                                                                   
In file included from /home1/sagendor/bin/src/NanoShaper0.7/src/sturm/sturm.cpp:8:0:                                                                                                                                                         
/usr/include/stdio.h:33:21: fatal error: stddef.h: No such file or directory                                                                                                                                                                 
 # include <stddef.h>                                                                                                                                                                                                                        
                     ^                                                                                                                                                                                                                       
compilation terminated.

It can’t find the file “stddef.h”. I have whatever module contians gcc loaded:
[sagendor@discovery build]$ gcc --version
gcc (GCC) 8.3.0

so I’m wondering if I am missing a module that would contain “stddef.h”?

1 Like

It looks like you’re using cmake for building, which by default does not seem to detect the gcc 8.3.0 from the module, but rather the system gcc. This is counterintuitive since the gcc on your path is from the module, hence the gcc --version output that you see. Look at your original cmake output though - it should state the version that it found within the first few lines of output. Under normal circumstances, using the system gcc should work (assuming the codebase supports such an old compiler version), but something about how the gcc module modifies the environment breaks the system gcc header search. I’ve been using a workaround to explicitly point cmake to the module’s gcc and g++, e.g.:

cmake .. -DCMAKE_C_COMPILER=/spack/apps/gcc/8.3.0/bin/gcc -DCMAKE_CXX_COMPILER=/spack/apps/gcc/8.3.0/bin/g++

I’d also be happy to know if there’s a better way.

Cheers.

5 Likes

For gcc:
CC=gcc CXX=g++ FC=gfortran cmake . ./ …[options]…

For intel:
CC=icc CXX=icpc FC=ifort cmake . ./ …[options]…

2 Likes

Thanks for your help! That fixed the first issue, but I ran into a second one. Here’s the output of my CMakeErrors.log:

Performing C SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output:
Change Dir: /home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/gmake cmTC_0e12d/fast && /usr/bin/gmake -f CMakeFiles/cmTC_0e12d.dir/build.make CMakeFiles/cmTC_0e12d.dir/build
gmake[1]: Entering directory `/home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_0e12d.dir/src.c.o
/spack/apps/gcc/8.3.0/bin/gcc   -DCMAKE_HAVE_LIBC_PTHREAD   -o CMakeFiles/cmTC_0e12d.dir/src.c.o   -c /home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp/src.c
Linking C executable cmTC_0e12d
/spack/apps/linux-centos7-x86_64/gcc-8.3.0/cmake-3.16.2-ejzgeuxnaazwznsodm7t422o5mgubobq/bin/cmake -E cmake_link_script CMakeFiles/cmTC_0e12d.dir/link.txt --verbose=1
/spack/apps/gcc/8.3.0/bin/gcc  -DCMAKE_HAVE_LIBC_PTHREAD    -rdynamic CMakeFiles/cmTC_0e12d.dir/src.c.o  -o cmTC_0e12d 
CMakeFiles/cmTC_0e12d.dir/src.c.o: In function `main':
src.c:(.text+0x2d): undefined reference to `pthread_create'
src.c:(.text+0x39): undefined reference to `pthread_detach'
src.c:(.text+0x4a): undefined reference to `pthread_join'
src.c:(.text+0x5e): undefined reference to `pthread_atfork'
collect2: error: ld returned 1 exit status
gmake[1]: *** [cmTC_0e12d] Error 1
gmake[1]: Leaving directory `/home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp'
gmake: *** [cmTC_0e12d/fast] Error 2


Source file was:
#include <pthread.h>

void* test_func(void* data)
{
  return data;
}

int main(void)
{
  pthread_t thread;
  pthread_create(&thread, NULL, test_func, NULL);
  pthread_detach(thread);
  pthread_join(thread, NULL);
  pthread_atfork(NULL, NULL, NULL);
  pthread_exit(NULL);

  return 0;
}

Determining if the function pthread_create exists in the pthreads failed with the following output:
Change Dir: /home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/gmake cmTC_55b7f/fast && /usr/bin/gmake -f CMakeFiles/cmTC_55b7f.dir/build.make CMakeFiles/cmTC_55b7f.dir/build
gmake[1]: Entering directory `/home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_55b7f.dir/CheckFunctionExists.c.o
/spack/apps/gcc/8.3.0/bin/gcc   -DCHECK_FUNCTION_EXISTS=pthread_create   -o CMakeFiles/cmTC_55b7f.dir/CheckFunctionExists.c.o   -c /spack/apps/linux-centos7-x86_64/gcc-8.3.0/cmake-3.16.2-ejzgeuxnaazwznsodm7t422o5mgubobq/share/cmake-3.16/Modules/CheckFunctionExists.c
Linking C executable cmTC_55b7f
/spack/apps/linux-centos7-x86_64/gcc-8.3.0/cmake-3.16.2-ejzgeuxnaazwznsodm7t422o5mgubobq/bin/cmake -E cmake_link_script CMakeFiles/cmTC_55b7f.dir/link.txt --verbose=1
/spack/apps/gcc/8.3.0/bin/gcc  -DCHECK_FUNCTION_EXISTS=pthread_create    -rdynamic CMakeFiles/cmTC_55b7f.dir/CheckFunctionExists.c.o  -o cmTC_55b7f  -lpthreads 
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
gmake[1]: *** [cmTC_55b7f] Error 1
gmake[1]: Leaving directory `/home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp'
gmake: *** [cmTC_55b7f/fast] Error 2

It’s complaining about the pthreads library… do you have any ideas how to fix this? I never had either of these issues when compiling on previous cluster…

CMake is not picking up the ‘/usr/lib64/libpthread.so’ library file it seems. How about trying including the following in your cmake options:

(1) LDFLAGS='-L/usr/lib64'

(2) -DCMAKE_CXX_IMPLICIT_LINK_LIBRARIES='pthread;stdc++;m;c'

(3) -DCMAKE_CXX_IMPLICIT_LINK_DIRECTORIES=/usr/lib64

Hi - I’m having a similar error for a different software installation. I’m trying to install angsd assuming a systemwide install of htslib (I have the htslib module loaded) - which should work according to the installation instructions http://www.popgen.dk/angsd/index.php/Installation
but I get the following error:


Is there some other module I need to install to get this working, or another flag I should be including?

Hi, I believe this is due to your Makefile using /usr/bin/cc as the default compiler. How to resolve this depends on individual makefiles but there should be a line that looks similar to

CC=cc

You can try editing it so that is says

CC=gcc

or you may have to set an environment variable before running make

export CC=gcc

If those suggestions don’t work, can you please attach a copy of your makefile?

It appears to specify gcc. I tried the envt variable solution but now its a different hts lib error

CC ?= gcc

CXX ?= g++

LIBS = -lz -lm -lbz2 -llzma -lpthread -lcurl

Adjust $(HTSSRC) to point to your top-level htslib directory

ifdef HTSSRC

$(info HTSSRC defined)

CPPFLAGS += -I$(realpath $(HTSSRC))

LIBS := $(realpath $(HTSSRC))/libhts.a $(LIBS)

else

$(info HTSSRC not defined, assuming systemwide installation)

LIBS += -lhts

endif

#modied from htslib makefile

FLAGS = -O3

FLAGS2 = $(CPPFLAGS) $(FLAGS) $(LDFLAGS)

CFLAGS := $(FLAGS2) $(CFLAGS)

CXXFLAGS := $(FLAGS2) $(CXXFLAGS)

#for compiling with ZSTD which is used for .bgen file format

ifeq ($(WITH_ZSTD),1)

LIBS += -lzstd

CXXFLAGS += -D__ZSTD__

endif

CSRC = $(wildcard *.c)

CXXSRC = $(wildcard *.cpp)

OBJ = $(CSRC:.c=.o) $(CXXSRC:.cpp=.o)

prefix = /usr/local

exec_prefix = $(prefix)

bindir = $(exec_prefix)/bin

INSTALL = install

INSTALL_DIR = $(INSTALL) -dm0755

INSTALL_PROGRAM = $(INSTALL) -Dm0755

#$(info CFLAGS=$(CFLAGS))

#$(info CXXFLAGS=$(CXXFLAGS))

PROGRAMS = angsd

all: $(PROGRAMS) misc

BAMDIR=""

BDIR=$(realpath $(BAMDIR))

PACKAGE_VERSION = 0.933

ifneq “$(wildcard .git)” “”

PACKAGE_VERSION := $(shell git describe --always --dirty)

version.h: $(if $(wildcard version.h),$(if $(findstring “$(PACKAGE_VERSION)”,$(shell cat version.h)),force))

endif

version.h:

echo ‘#define ANGSD_VERSION “$(PACKAGE_VERSION)”’ > $@

.PHONY: all clean install install-all install-misc misc test

misc: analysisFunction.o bfgs.o prep_sites.o

$(MAKE) -C misc HTSSRC=$(realpath $(HTSSRC))

-include $(OBJ:.o=.d)

%.o: %.c

$(CC) -c $(CFLAGS) $*.c

$(CC) -MM $(CFLAGS) $.c >$.d

%.o: %.cpp

$(CXX) -c $(CXXFLAGS) $*.cpp

$(CXX) -MM $(CXXFLAGS) $.cpp >$.d

angsd: version.h $(OBJ)

$(CXX) $(FLAGS) -o angsd *.o $(LIBS)

testclean:

rm -rf test/sfstest/output test/tajima/output test/*.log version.h test/temp.txt

clean: testclean

rm -f *.o *.d $(PROGRAMS) version.h *~

$(MAKE) -C misc clean

test:

echo “Only subset of analyses is being tested”

cd test;./testAll.sh …/angsd $(BDIR)

force:

install: all

$(INSTALL_DIR) $(DESTDIR)$(bindir)

$(INSTALL_PROGRAM) $(PROGRAMS) $(DESTDIR)$(bindir)

$(MAKE) -C misc HTSSRC=$(realpath $(HTSSRC)) install

install-misc: misc

$(MAKE) -C misc HTSSRC=$(realpath $(HTSSRC)) install-misc

install-all: install install-misc

The makefile is looking for htslib. Luckily we have a module for that installed so you should be able to get it working like so:

module load htslib
export HTSSRC=$HTSLIB_ROOT

alas, back to the original error

I have this software running on the old hpc - when I did the htslib install locally I had to install xz first from https://tukaani.org/xz/ and then use it in the htslib install. But I’d really like to be able to use the module system on discovery…

Alas, indeed! I’ll create a new post on how to build angsd (using the latest version of the source code) so this thread doesn’t go off topic. We can continue the conversation there. How to: Build ANGSD

For people in the future, it’s a problem with the makefile trying to build with system libraries instead of modules.

1 Like

So I gave up in favor of spending my time on more productive things, but now I really need this software compiled and still no success…

This is the exact cmake command I have run:

cmake . -DCMAKE_CXX_IMPLICIT_LINK_LIBRARIES="pthread;stdc++;m;c" -DCMAKE_CXX_IMPLICIT_LINK_DIRECTORIES=/usr/lib64 -DBOOST_ROOT=/spack/apps/linux-centos7-x86_64/gcc-8.3.0/boost-1.70.0-p76g23etpc3e56qm3yw3c2qf4cahu5ek -DCMAKE_C_COMPILER=/spack/apps/gcc/8.3.0/bin/gcc -DCMAKE_CXX_COMPILER=/spack/apps/gcc/8.3.0/bin/g++ -DWITH_examples=false -DWITH_CGAL_Qt4=false -DWITH_CGAL_Qt3=false -DWITH_CGAL_ImageIO=false > cmake_cgal.txt

And the following env variables are set:

[sagendor@discovery CMakeFiles]$ echo $CC; echo $CXX; echo $LDFLAGS
gcc
g++
-L/usr/lib64

I still get an error related to ‘pthreads’:

Performing C SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output:
Change Dir: /home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/gmake cmTC_f679d/fast && /usr/bin/gmake -f CMakeFiles/cmTC_f679d.dir/build.make CMakeFiles/cmTC_f679d.dir/build
gmake[1]: Entering directory `/home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_f679d.dir/src.c.o
/spack/apps/gcc/8.3.0/bin/gcc   -DCMAKE_HAVE_LIBC_PTHREAD   -o CMakeFiles/cmTC_f679d.dir/src.c.o   -c /home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp/src.c
Linking C executable cmTC_f679d
/spack/apps/linux-centos7-x86_64/gcc-8.3.0/cmake-3.16.2-ejzgeuxnaazwznsodm7t422o5mgubobq/bin/cmake -E cmake_link_script CMakeFiles/cmTC_f679d.dir/link.txt --verbose=1
/spack/apps/gcc/8.3.0/bin/gcc  -DCMAKE_HAVE_LIBC_PTHREAD    -rdynamic CMakeFiles/cmTC_f679d.dir/src.c.o  -o cmTC_f679d 
CMakeFiles/cmTC_f679d.dir/src.c.o: In function `main':
src.c:(.text+0x2d): undefined reference to `pthread_create'
src.c:(.text+0x39): undefined reference to `pthread_detach'
src.c:(.text+0x4a): undefined reference to `pthread_join'
src.c:(.text+0x5e): undefined reference to `pthread_atfork'
collect2: error: ld returned 1 exit status
gmake[1]: *** [cmTC_f679d] Error 1
gmake[1]: Leaving directory `/home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp'
gmake: *** [cmTC_f679d/fast] Error 2


Source file was:
#include <pthread.h>

void* test_func(void* data)
{
  return data;
}

int main(void)
{
  pthread_t thread;
  pthread_create(&thread, NULL, test_func, NULL);
  pthread_detach(thread);
  pthread_join(thread, NULL);
  pthread_atfork(NULL, NULL, NULL);
  pthread_exit(NULL);

  return 0;
}

Determining if the function pthread_create exists in the pthreads failed with the following output:
Change Dir: /home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/gmake cmTC_2e213/fast && /usr/bin/gmake -f CMakeFiles/cmTC_2e213.dir/build.make CMakeFiles/cmTC_2e213.dir/build
gmake[1]: Entering directory `/home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_2e213.dir/CheckFunctionExists.c.o
/spack/apps/gcc/8.3.0/bin/gcc   -DCHECK_FUNCTION_EXISTS=pthread_create   -o CMakeFiles/cmTC_2e213.dir/CheckFunctionExists.c.o   -c /spack/apps/linux-centos7-x86_64/gcc-8.3.0/cmake-3.16.2-ejzgeuxnaazwznsodm7t422o5mgubobq/share/cmake-3.16/Modules/CheckFunctionExists.c
Linking C executable cmTC_2e213
/spack/apps/linux-centos7-x86_64/gcc-8.3.0/cmake-3.16.2-ejzgeuxnaazwznsodm7t422o5mgubobq/bin/cmake -E cmake_link_script CMakeFiles/cmTC_2e213.dir/link.txt --verbose=1
/spack/apps/gcc/8.3.0/bin/gcc  -DCHECK_FUNCTION_EXISTS=pthread_create    -rdynamic CMakeFiles/cmTC_2e213.dir/CheckFunctionExists.c.o  -o cmTC_2e213  -lpthreads 
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
gmake[1]: *** [cmTC_2e213] Error 1
gmake[1]: Leaving directory `/home1/sagendor/bin/src/NanoShaper0.7/CGAL-4.2-beta1/CMakeFiles/CMakeTmp'
gmake: *** [cmTC_2e213/fast] Error 2

Any other ideas?

Yeah well welcome to the world of building codes- it can get complicated. Can you provide details about the code that you are trying to build- a github link maybe? I guess that you are trying to build a beta version of cgal.

A quick web search took me to a magic place called stackoverflow. It may not solve the issue, but you can try the suggestions posted on the following link:
Link: https://stackoverflow.com/questions/39598323/how-to-properly-link-libraries-with-cmake

The build protocol using cmake is to add the following to the ‘CMakeLists.txt’ file:
target_link_libraries(-lpthread)
– or –
target_link_libraries(/usr/lib64/libpthread.so)
– or –
target_link_libraries(-L/usr/lib64)

It would be nice if it were that easy, but where to find the right place to make the additions to this file:
Link: https://github.com/CGAL/cgal/blob/master/Installation/CMakeLists.txt

Lets try the easiest thing first- can you try exporting the following environment variable before running your cmake command:
export LD_PRELOAD=/usr/lib64/libpthread.so

If that doesn’t work, lets try adding the following to your cmake command:
CMAKE_EXE_LINKER_FLAGS=-lpthread CGAL_EXE_LINKER_FLAGS="-library=pthread"

Neither worked unfortunately. Yes, I am trying to compile CGAL but only as a dependency for the actual software I am trying to install, and this is actually a patched version of CGAL, so I need to do it manually and I don’t know if the patch will work on other versions.

When I exported the LD_PRELOAD command, I’m now getting spammed with the following error whenever I run any system command (e.g. cd or ls)

ERROR: ld.so: object '/usr/lib64/libpthread.so' from LD_PRELOAD cannot be preloaded: ignored.

I don’t really understand why this is so difficult. All of this code compiled flawlessly on the previous system, so I assume this is related to the new module system?

Do you know what the root issue is here and if so can you explain so that I have a better understanding of what I should be trying?

So I had a look in /usr/lib64 and /lib64 and I noticed that the permissions for “libpthread.so” are different than most other *.so files in this directory:

[sagendor@discovery lib64]$ ls -lt libpthread*
lrwxrwxrwx 1 root root     18 Aug 18 14:40 libpthread.so.0 -> libpthread-2.17.so
-rw-r--r-- 1 root root   1764 Mar 31  2020 libpthread_nonshared.a
-rwxr-xr-x 1 root root 142144 Mar 31  2020 libpthread-2.17.so
-rw-r--r-- 1 root root    222 Mar 31  2020 libpthread.so

Could this be related? There is no execute permissions?

From the cmake documentation for the newer cmake releases:
In-source builds are not allowed.
You should create separate directory for build files.

Can you try the following:

module load cmake/3.16.2

mkdir build; cd build

CC=gcc CXX=g++ FC=gfortran CFLAGS="-O3 -pthread" CXXFLAGS="-O3 -pthread" FFLAGS="-O3 -pthread" cmake . ./ -DBOOST_ROOT=/spack/apps/linux-centos7-x86_64/gcc-8.3.0/boost-1.70.0-p76g23etpc3e56qm3yw3c2qf4cahu5ek -DWITH_examples=false -DWITH_CGAL_Qt4=false -DWITH_CGAL_Qt3=false -DWITH_CGAL_ImageIO=false

I also got a similar problem. The problem with cmake is that it failed to find the correct gcc we want.
And in my case, only this works

cmake … -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++

CC=gcc CXX=g++ doesn’t help for me.

1 Like