Using pthread in a MPI program has a Compilation error - c++

I'm trying to run an MPI program that uses pthreads. I am able to compile and run in my local machine but couldn't do that on a server.
This is the command I used to compile.
target1: TARGET=main
target2: TARGET=kmer_finaliser
CC = mpic++
# CPPFLAGS = -pg
# CPPFLAGS = -lm -g -Wall -pthread
CPPFLAGS = -std=c++11 -pthread
USER_LIBS = -I /home/ruchin/sparsehash-sparsehash-2.0.4/src
main: main.o extractor.o com.o kmer_dump.o thread_pool.o
$(CC) $(CPPFLAGS) -o kmer_counter.out main.o extractor.o com.o kmer_dump.o thread_pool.o $(USER_LIBS)
main.o: main.cpp extractor.h com.h kmer_dump.h thread_pool.h
$(CC) $(CPPFLAGS) -c main.cpp $(USER_LIBS)
kmer_finaliser: kmer_finaliser.o kmer_dump.o
$(CC) $(CPPFLAGS) -o kmer_finaliser.out kmer_finaliser.o kmer_dump.o $(USER_LIBS)
kmer_finaliser.o: kmer_finaliser.cpp kmer_dump.h
$(CC) $(CPPFLAGS) -c kmer_finaliser.cpp $(USER_LIBS)
extractor.o: extractor.cpp extractor.h
$(CC) $(CPPFLAGS) -c extractor.cpp $(USER_LIBS)
com.o: com.cpp com.h
$(CC) $(CPPFLAGS) -c com.cpp $(USER_LIBS)
kmer_dump.o: kmer_dump.cpp kmer_dump.h
$(CC) $(CPPFLAGS) -c kmer_dump.cpp $(USER_LIBS)
thread_pool.o: thread_pool.cpp kmer_dump.h
$(CC) $(CPPFLAGS) -c thread_pool.cpp $(USER_LIBS)
This is the error I get.
/usr/bin/ld: thread_pool.o: undefined reference to symbol 'pthread_create##GLIBC_2.2.5'
/lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

According to the man page of g++, you should include also in the compilation the flag -pthread:
-pthread
Define additional macros required for using the POSIX threads
library. You should use this option consistently for both
compilation and linking. This option is supported on
GNU/Linux targets, most other Unix derivatives, and also on
x86 Cygwin and MinGW targets.

Related

"DSO missing from command line" while cross compiling mosquitto example

I am trying to cross-compile the mosquitto example program. At first I compiled the mosquitto example on Host PC, it worked well. The makefile is as following:
CFLAGS=-Wall -ggdb -I../../lib -I../../lib/cpp
LDFLAGS=-L../../lib ../../lib/cpp/libmosquittopp.so.1 ../../lib/libmosquitto.so.1
.PHONY: all clean
all : mqtt_temperature_conversion
mqtt_temperature_conversion : main.o temperature_conversion.o
${CXX} $^ -o $# ${LDFLAGS}
main.o : main.cpp
${CXX} -c $^ -o $# ${CFLAGS}
temperature_conversion.o : temperature_conversion.cpp
${CXX} -c $^ -o $# ${CFLAGS}
clean :
-rm -f *.o mqtt_temperature_conversion
Then I add the following lines to cross compile this program:
CXX=arm-unknown-linux-gnueabi-gcc
AR=arm-unknown-linux-gnueabi-ar
AS=arm-unknown-linux-gnueabi-as
LD=arm-unknown-linux-gnueabi-ld
RANLIB=arm-unknown-linux-gnueabi-ranlib
Then it gives a error message:
/home/Tools/tool_chain/bin/../lib/gcc/arm-unknown-linux-
gnueabi/5.4.0/../../../../arm-unknown-linux-gnueabi/bin/ld: main.o:
undefined reference to symbol '_ZdlPv##GLIBCXX_3.4'
/home/Tools/tool_chain/bin/../sysroot/usr/lib/libstdc++.so.6:
error adding symbols: DSO missing from command line
I am very confused. I googled for this error, but none of them seems to be relevant. Cause when I compiled with the host PC compiler, everything works. So it should not be a lib problem.
update:
So I could not figure out what happened, then I made a test project myself. The Makefile is as follows:(I ignored the cross tool chain variabl parts to save some place)
CFLAGS=-Wall -ggdb -I../../lib -I../../lib/cpp
LDFLAGS=-L../../lib ../../lib/cpp/libmosquittopp.so.1 ../../lib/libmosquitto.so.1
mqtt: main.o
${CXX} $^ -o $# ${LDFLAGS}
main.o: main.cpp
${CXX} -c $^ -o $# ${CFLAGS}
And it worked?! I flashed it to the board the executable seems to be correct.
Now I am more confused
LD=arm-unknown-linux-gnueabi-ld
Don't use ld directly, ever. You want to use g++ as the driver that calls your linker, otherwise you will have linker problems.
LD=$(CXX)

c++ undefined reference to `main' while compiling

After try to use this attached makefile the error that appears is :
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [Ass1F] Error 1
the makefile is :
all: Ass1F
Ass1F: bin/main.o bin/x.o bin/y.o bin/z.o bin/w.o
g++ -o bin/main.o bin/x.o bin/y.o bin/z.o bin/w.o
#echo 'Finished building target: Ass1F'
#echo ' '
bin/x.o: src/x.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/x.o src/x.cpp
bin/y.o: src/y.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/y.o src/y.cpp
bin/z.o: src/z.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/z.o src/z.cpp
bin/w.o: src/w.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/w.o src/w.cpp
bin/main.o: src/main.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/main.o src/main.cpp
clean:
rm -f bin/*
what should I do with this problem?
the reason is the makefile or something in the code?
just to let you know we used eclipse to write the code and everything work perfectly- no any bugs.
thanks
Line g++ -o bin/main.o bin/x.o bin/y.o bin/z.o bin/w.o tries to create executable named bin/main.o, overwriting its previous contents.
It should be e.g. g++ -o Ass1F bin/main.o bin/x.o bin/y.o bin/z.o bin/w.o
Your makefile recipe for Ass1F is incorrect, it doesn't specify the output file, os make uses bin/main.o as the output file instead of one of the input files.
You can simplify the makefile by using pattern rules (i.e. with wildcards) and using pre-defined variables such as $# (the name of the target) and $^ (the list of prerequisites) and CXX (the C++ compiler):
CXXFLAGS = -g -Wall -Weffc++
CPPFLAGS =
LDFLAGS =
LDLIBS =
all: Ass1F
Ass1F: bin/main.o bin/x.o bin/y.o bin/z.o bin/w.o
$(CXX) $(LDFLAGS) -o $# $^ $(LDLIBS)
#echo 'Finished building target: Ass1F'
#echo ' '
bin/%.o: src/%.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $# $^
clean:
rm -f bin/*

Statically link against pocketsphinx (Library)

I am developing a little program with pocketsphinx (speech to text - library).
On Windows i was using Code::Blocks as development environment and i had success to build a program.
Now i try to port my program to Linux and i am having little problems to link against pocketsphinx.
This is the Makefile:
CC = g++
CFLAGS = -Wall -std=c++11
LDFLAGS = -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx
OBJ = obj/Application.o obj/Main.o obj/Recorder.o
all: $(OBJ)
$(CC) -L/usr/local/lib -o bin/Eve $(OBJ) -s -lsphinxbase -lpocketsphinx
obj/Main.o: src/Main.cpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Main.cpp -o obj/Main.o
obj/Application.o: src/Application.cpp src/Application.hpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Application.cpp -o obj/Application.o
obj/Recorder.o: src/Recorder.cpp src/Recorder.hpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Recorder.cpp -o obj/Recorder.o
It is the same which i was using on Windows, i just adjusted the file path.
I am receiving the following error:
$ make
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Application.cpp -o obj/Application.o
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Main.cpp -o obj/Main.o
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Recorder.cpp -o obj/Recorder.o
g++ -L/usr/local/lib -o bin/Eve obj/Application.o obj/Main.o obj/Recorder.o -s -lsphinxbase -lpocketsphinx
obj/Recorder.o: In function `Recorder::Recorder()':
Recorder.cpp:(.text+0x1c0): undefined reference to `ad_open_sps'
Recorder.cpp:(.text+0x20d): undefined reference to `ad_read'
Recorder.cpp:(.text+0x215): undefined reference to `cont_ad_init'
obj/Recorder.o: In function `Recorder::~Recorder()':
Recorder.cpp:(.text+0x2f3): undefined reference to `cont_ad_close'
Recorder.cpp:(.text+0x303): undefined reference to `ad_close'
obj/Recorder.o: In function `Recorder::recognizeFromMicrophone()':
Recorder.cpp:(.text+0x37a): undefined reference to `ad_start_rec'
Recorder.cpp:(.text+0x395): undefined reference to `cont_ad_calib'
Recorder.cpp:(.text+0x3f0): undefined reference to `cont_ad_read'
Recorder.cpp:(.text+0x4e6): undefined reference to `cont_ad_read'
Recorder.cpp:(.text+0x5b5): undefined reference to `ad_stop_rec'
Recorder.cpp:(.text+0x5d8): undefined reference to `ad_read'
Recorder.cpp:(.text+0x5f4): undefined reference to `cont_ad_reset'
collect2: error: ld returned 1 exit status
make: *** [all] Fehler 1
I don't think that it is a name mangling problem, since i built the lib on my own using the provided Makefile.
What can i do to link against the lib without errors?
EDIT: I figured out how to make it work. I simply modified the rule of the target "all" to this:
$(CC) -static -L/usr/local/lib -o bin/Eve $(OBJ) -s -lpocketsphinx -lsphinxbase -lsphinxad -lpthread
Functions like ad_read are defined in libsphinxad library, you need to add it to your linker command line:
g++ -static -L/usr/local/lib -o bin/Eve obj/Application.o obj/Main.o obj/Recorder.o \
-lpocketsphinx -lsphinxbase -libsphinxad
Please note that the order of libraries is important.

g++ consolidation: ld: fatal: file main.o: unknown file type

I'm scanning the web and all my project files for solution but still can't find the answer why my linker won't finish the job. Everything smoothly compiles into .o files, but the last make command fails. And here is the Makefile content:
CXX = g++
CXXFLAGS = -Wall -pedantic -c
OBJS = main.o operacje.o porownaj.o
dzialania: $(OBJS)
$(CXX) $^ -o $#
main.o: main.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) $^ -o $#
operacje.o: operacje.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) $^ -o $#
porownaj.o: porownaj.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) $^ -o $#
clean:
rm -f *o
and again, here is the mistake that pops out:
g++ main.o operacje.o porownaj.o -o dzialania
ld: fatal: file main.o: unknown file type
ld: fatal: file processing errors. No output written to dzialania
*** Error code 1
make: Fatal error: Command failed for target `dzialania'
I'm sure it's some kind of a basic mistake but after staring at the file for a few hours I won't notice it anyway. Maybe some of you folks with notice the bug with a fresh eye.
btw. it's my first post after long-term passive lurking, I hope I did everything right. Thanks in advance!
#edit1 OK, I did all the suggested corrections
#edit2 Seems like the problem is caused by improper module division of my program. I'll rearrange it's structure and let you know if it works then. Thanks for all the support!
#edit3 OK, I changed the structure of my program and everything runs smooth, Thanks again!
Try using $< instead of $^ in your rules to compile main.o, operacje.o, and porownaj.o:
main.o: main.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) $< -o $#
operacje.o: operacje.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) $< -o $#
porownaj.o: porownaj.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) $< -o $#
That will cause make to compile only the corresponding .cpp file. When you use $^ the header files are passed to the g++ command which tells the compiler to create precompiled headers for them - that's what's ending up in main.o instead of the object file for main.cpp.
GNU make variable definitions like CC = g++, or CFLAGS = -Wall -pedantic etc.. should each be on its own line:
CC = g++
CFLAGS = -Wall -pedantic
OBJS = main.o operacje.o porownaj.o
BTW, you probably mean
CXX = g++
CXXFLAGS = -Wall -pedantic
You certainly don't want -c explicitly in your CFLAGS or CXXFLAGS; you really should remove it.
Also, recipes should be after its rule, so you want
dzialania: $(OBJS)
$(LINK.cc) $^ -o $#
operacje.o: operacje.cpp operacje.h porownaj.h
$(CXX) $(CXXFLAGS) -c $< -o $#
The several spaces are actually a single tab character.
Run make -p to understand the rules known by make; see also this answer and that one.
Take time to read GNU make documentation.

Undefined reference to SDL_*many*

This is my makefile:
CFLAGS = -Wall -pedantic -pedantic-errors -g -lSDL -lSDL_image
CC = g++
OBJ = Point.o Personaje.o Juego.o EventHandler.o
all: main
#%.o: %.cpp %.h
# $(CC) -c $(CFLAGS) $<
bin: other $(OBJ)
Point.o: Point.cpp Point.h
$(CC) -c $(CFLAGS) $<
Personaje.o: Personaje.cpp Personaje.h
$(CC) -c $(CFLAGS) $<
Juego.o: Juego.cpp Juego.h
$(CC) -c $(CFLAGS) $<
EventHandler.o: EventHandler.cpp EventHandler.h
$(CC) -c $(CFLAGS) $<
main: $(OBJ)
cd ../vista/; make
cp ../vista/*.o .
rm ../vista/*.o
g++ $(CFLAGS) $(OBJ) Pantalla.o Imagen.o main.cpp -o main
clean:
rm *.o;
rm main
PHONY: main
When I compile with "make" I get the following errors:
EventHandler.cpp:17: undefined reference to `SDL_PollEvent'
Pantalla.o: In function `Pantalla::initialize(int, int, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
Pantalla.cpp:6: undefined reference to `SDL_Init'
Pantalla.cpp:9: undefined reference to `SDL_SetVideoMode'
Pantalla.cpp:14: undefined reference to `SDL_WM_SetCaption'
and many other errors.
The header of "Pantalla.h" contains this line:
#include "SDL/SDL.h"
And I already installed the SDL packages.
I don't know what I'm doing wrong... please help!
CFLAGS = -Wall -pedantic -pedantic-errors -g -lSDL -lSDL_image
This isn't good. You're mixing compiler flags (warnings and debug) with linker information (which libraries you need to link to). Use two separate variables for that:
CFLAGS = -Wall -pedantic -pedantic-errors -g
LIBS = -lSDL -lSDL_image
Then when you compile (but don't link, i.e. when you generate the .o files), only use $(CFLAGS) (like you have now). But when you do link (to produce the final executable), add the linker directives.
g++ $(CFLAGS) -o main main.cpp $(OBJ) Pantalla.o Imagen.o $(LIBS)
The order you put your objects and libraries is important.
One problem is that you are using exactly the same flags for compilation and linking. The compiler doesn't use libraries; the linker doesn't know about pedantic.
A bigger problem is that your -lSDL -lSDL_image appears too early in the link command, g++ $(CFLAGS) $(OBJ) Pantalla.o Imagen.o main.cpp -o main. The linker won't look into libSDL.a or libSDL_image.a (or their equivalents) because there are no unresolved external references at the point where you specified the search. Put the -lSDL -lSDL_image options just before the -o option.