Building soloud example with emscripten not working - c++

I am trying to port a project of mine to the web with emscripten, and one of the libraries I use in it is soloud. Building the library itself compiles fine with no errors, but when I try to compile one of the examples with it, I get a bunch of compiler errors.
Here are the steps I use to build the library:
Download and build genie
Download soloud and put genie executable in build folder
Run ./genie --with-miniaudio-only gmake
cd into gmake directory and run emmake make
This compiles fine with no errors, however when I try to build the simplest example with emcc main.cpp libsoloud_static.a -I ../include -o index.html, I get these errors
emcc: warning: libsoloud_static.a: archive is missing an index; Use emar when creating libraries to ensure an index is created [-Wemcc]
emcc: warning: libsoloud_static.a: adding index [-Wemcc]
error: undefined symbol: _ZN6SoLoud6Soloud19getActiveVoiceCountEv (referenced by top-level compiled C/C++ code)
warning: Link with `-sLLD_REPORT_UNDEFINED` to get more information on undefined symbols
warning: To disable errors for undefined symbols use `-sERROR_ON_UNDEFINED_SYMBOLS=0`
warning: __ZN6SoLoud6Soloud19getActiveVoiceCountEv may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN6SoLoud6Soloud4initEjjjjj (referenced by top-level compiled C/C++ code)
warning: __ZN6SoLoud6Soloud4initEjjjjj may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN6SoLoud6Soloud4playERNS_11AudioSourceEffbj (referenced by top-level compiled C/C++ code)
warning: __ZN6SoLoud6Soloud4playERNS_11AudioSourceEffbj may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN6SoLoud6Soloud6deinitEv (referenced by top-level compiled C/C++ code)
warning: __ZN6SoLoud6Soloud6deinitEv may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN6SoLoud6SoloudC1Ev (referenced by top-level compiled C/C++ code)
warning: __ZN6SoLoud6SoloudC1Ev may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN6SoLoud6SoloudD1Ev (referenced by top-level compiled C/C++ code)
warning: __ZN6SoLoud6SoloudD1Ev may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN6SoLoud6Speech7setTextEPKc (referenced by top-level compiled C/C++ code)
warning: __ZN6SoLoud6Speech7setTextEPKc may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN6SoLoud6SpeechC1Ev (referenced by top-level compiled C/C++ code)
warning: __ZN6SoLoud6SpeechC1Ev may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN6SoLoud6SpeechD1Ev (referenced by top-level compiled C/C++ code)
warning: __ZN6SoLoud6SpeechD1Ev may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: _ZN6SoLoud6Thread5sleepEi (referenced by top-level compiled C/C++ code)
warning: __ZN6SoLoud6Thread5sleepEi may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
Error: Aborting compilation due to previous errors
What am I doing wrong?

UPDATE:
I was able to get it to work by changing this
CC = gcc
CXX = g++
AR = ar
to this
CC = emcc
CXX = em++
AR = emar
and this
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP -MP $(DEFINES) $(INCLUDES)
ALL_ASMFLAGS += $(ASMFLAGS) $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -g -msse4.1 -fPIC
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -g -msse4.1 -fPIC
ALL_CXXFLAGS += $(CXXFLAGS) $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -g -fno-exceptions -fno-rtti -msse4.1 -fPIC
ALL_OBJCFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -g -msse4.1 -fPIC
ALL_OBJCPPFLAGS += $(CXXFLAGS) $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -g -fno-exceptions -fno-rtti -msse4.1 -fPIC
to this
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP -MP $(DEFINES) $(INCLUDES)
ALL_ASMFLAGS += $(ASMFLAGS) $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -g -fPIC
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -g -fPIC
ALL_CXXFLAGS += $(CXXFLAGS) $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -g -fno-exceptions -fno-rtti -fPIC
ALL_OBJCFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -g -fPIC
ALL_OBJCPPFLAGS += $(CXXFLAGS) $(CFLAGS) $(ALL_CPPFLAGS) $(ARCH) -g -fno-exceptions -fno-rtti -fPIC
(still not sure why I had to remove the -msse4.1 flag, but I was getting a weird error)
in SoloudStatic.make. For some reason it was still using a regular compiler instead of emscripten.

Related

Igraph makevars will not link to static library, i can use data structures but cannot functions while importing igraph c++ library

I am trying to install my C++ igraph library from https://github.com/igraph/igraph to visual studio code using the following method this is my makefile made according to this link.
CXX = g++
CXX_FLAGS = -std=c++17 -O3 -march=native -DNDEBUG
LIB = -Llib
INC = -Iinclude
.PHONY: all
all: a.out
a.out: main.cpp
$(CXX) $(CXX_FLAGS) $(INC) $(LIB) -ligraph -lm -lstdc++ -lgomp -lpthread -o $# main.cpp
.PHONY: clean
clean:
rm a.out
The compiler will always return something like:
g++ -std=c++17 -O3 -march=native -DNDEBUG -Iinclude -Llib -ligraph -lm -lstdc++ -lgomp -lpthread -o a.out main.cpp
/usr/bin/ld: /tmp/ccqJLfvi.o: in function `main':
main.cpp:(.text.startup+0x9): undefined reference to `igraph_rng_default'
/usr/bin/ld: main.cpp:(.text.startup+0x16): undefined reference to `igraph_rng_seed'
collect2: error: ld returned 1 exit status
make: *** [Makefile:12: a.out] Error 1
If i only want to use data structures such as igraph_t graph* it will work, but if i try to call fucntion it will return error and will not generate a.out file. It would be incredablly good if someone would be able to explain why this happens cuz it really got on my nerve right now.
Please follow the instructions in the documentation to set up your package to link to igraph.
Instructions to install igraph: https://igraph.org/c/html/latest/igraph-Installation.html Note that you must both build and install the package. Make a note of the location you used to install it to (the value of CMAKE_INSTALL_PREFIX)
Instructions on compiling your first igraph program: https://igraph.org/c/html/latest/igraph-Tutorial.html Unless you are already comfortable with writing C programs and linking them to external libraries, I strongly recommend that you use CMake to set up your project, as described in the linked tutorial. CMake works the same way on all platforms (Windows/macOS/Linux) and will automatically figure out how to link your program to igraph correctly. When configuring your project, be sure to set CMAKE_PREFIX_PATH to the location where you installed igraph earlier.

Source code files compiles to object files individually, but the object files don't compile together

I have a C++ project which uses multiple external static libraries and a huge code base.
If I put it all together like this:
g++ (ALL INCLUDE PATHS) (FLAGS) (ALL LIBRARIES) (ALL SOURCES) -o FILE
All at once in the same command, it works.
However I want to use a Makefile to generate file objects and speed up the compiling process.
I am able to generate a object file for each individual source file, but when trying to put them all together like this:
g++ -o FILE (ALL OBJECT FILES FOUND RECURSIVELY IN THE PATH)
I get the following errors, as if they had not been compiled with the static libraries
Linux_Release/.compilation_libs/linux.o: In function `WndProc(int, int, int)':
main_Linux.cpp:(.text+0xc6): undefined reference to `XRefreshKeyboardMapping'
main_Linux.cpp:(.text+0x18c): undefined reference to `XLookupKeysym'
main_Linux.cpp:(.text+0x20b): undefined reference to `Xutf8LookupString'
...
I've tried to put the includes/flags/libraries back into the command once again, but it doesn't make a difference.
Here are the flags I'm using for compiling each source file individually:
CFLAGS := -fmessage-length=0 -std=c++11 -Wno-sign-compare
-Wno-unused-local-typedefs -Wno-reorder -Wno-switch -fpermissive -static-libstdc++ -static-libgc
c -Wl,-rpath=.
And here are some of the libs included in the command above
LIBS := -lcurl -lz -lX11 -lXi -lGL -lGLU -lGLEW -lfreetype -lbass -lbass_fx -lOpenCL (and more...)
And the target for building each file object looks like this:
$(COMPILATION_LIBS_FOLDER)/%.o: %.cpp
mkdir -p '$(#D)'
g++ $(CFLAGS) $(FIXED_INCLUDES) $(LIBPATH) $(LIBS) -c $< -o $#

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.

/usr/bin/ld: cannot find : No such file or directory

I'm following this SDL tutorial to try and make use of some SDL extension libraries. My code is identical to theirs but I am still unable to make the file which leads me to believe the problem is in my makefile which looks like this:
CXX = g++
# Update these paths to match your installation
# You may also need to update the linker option rpath, which sets where to look for
# the SDL2 libraries at runtime to match your install
SDL_LIB = -L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib, -lSDL2_image
SDL_INCLUDE = -I/usr/local/include
# You may need to change -std=c++11 to -std=c++0x if your compiler is a bit older
CXXFLAGS = -Wall -c -std=c++11 $(SDL_INCLUDE)
LDFLAGS = $(SDL_LIB)
EXE = SDL_Lesson3
all: $(EXE)
$(EXE): main.o
$(CXX) $< $(LDFLAGS) -o $#
main.o: main.cpp
$(CXX) $(CXXFLAGS) $< -o $#
clean:
rm *.o && rm $(EXE)
That makefile worked fine for previous examples. The only thing that has changed in this example is line 5 where I added -lSDL2_image as per the tutorial. When I try make the file I get the following traceback:
rony#comet:~/Documents/cpp/helloworld/lesson3$ make
g++ main.o -L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib, -lSDL2_image -o SDL_Lesson3
/usr/bin/ld: cannot find : No such file or directory
collect2: error: ld returned 1 exit status
make: *** [SDL_Lesson3] Error 1
Is there an error with my makefile? Have I not installed the library correctly?
The problem is this rogue comma:
SDL_LIB = -L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib, -lSDL2_image
^
causing the linker to look for libraries in a non-existent directory with an empty name, as well as /usr/local/lib. Removing the comma should fix it.

Linking against static `a` lib leads to 'undefined reference' when dynamic `so` lib does not

I had to build ImageMagick from sources to get around a problem I was having. Having done that I can now compile my code with the new shared library, which incidentally is called libMagick++-6.Q8.so. This I can do without a problem.
What I need is to be able to easily move the binary to other machines without having to rely on the (custom built) shared library, but building my code against the libMagick++-6.Q8.a file leads to numerous undefined reference linker errors.
Here's the relevant lines from my Makefile:
CXX= g++
CFLAGS= -DRENDER_TO_TEX -DUSEMAGICK -DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=8 -O2 -pthread
LIBS= -L/opt/vc/lib -L/usr/lib -lbcm_host -lEGL -lGLESv2 -lstdc++
LIBS+= /usr/lib/libjsoncpp.a /usr/lib/libboost_regex.a /usr/local/lib/libMagick++-6.Q8.a
INCS= -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -I/usr/local/include/ImageMagick-6
slideshow: slideshow.cpp $(OBJS)
$(CXX) $(CFLAGS) slideshow.cpp ${INCS} ${LIBS} ${OBJS} -o $#