Bob van Luijt
Bob van Luijt

Reputation: 7598

How to set gcc flags in Emscripten

I compile with the following command:

gcc -Wall -march=native -O3 -ffast-math -I/usr/local/include -I/usr/local/include -o waon main.o notes.o midi.o analyse.o fft.o hc.o snd.o -L/usr/local/lib -L/usr/local/lib -lfftw3 -L/usr/local/lib -lsndfile -lm

I now would like to compile with Emscripten. How do I convert the above gcc command into an emcc command?

Upvotes: 2

Views: 2193

Answers (2)

Nikola Lukic
Nikola Lukic

Reputation: 4246

Make research about emsdk download&setup on your computer.

Download emsdk instruction

Next interest link is :

emcc or em++ instruction https://emscripten.org/docs/tools_reference/emcc.html

When you setup emcc in command line you can see this project (i make emcc final look based on python script runner.py etc.):

c-cpp-to-javascript

Basic and useful example's :

Pretty analog with gcc :

Args:

-lGL for openGL

-s TOTAL_MEMORY=512MB --memory-init-file 1 Memory staff

--preload-file folderWithImages/--use-preload-plugins If you use assets

-I forInclude/someheader.h

-L libraryFolder/someLib.lib

-std=c11

Simple run:

./emcc -O2 a.cpp -o a.js

or

./emcc -O2 a.cpp -o a.html

Links:

./emcc -O2 a.cpp -o a.bc ./emcc -O2 b.cpp -o b.bc ./emcc -O2 a.bc b.bc -o project.js

Or :

  • to get JS

    emcc -s WASM=1 myAdds.a myLib.a source1.c source2.cpp -o build.js

  • to get html

    emcc -s WASM=1 myAdds.a myLib.a source1.c source2.cpp -o build.html

Link together the bitcode files:

emcc project.bc libstuff.bc -o allproject.bc

Compile the combined bitcode to HTML

emcc allproject.bc -o final.html

Important note :

You can't take an existing .a library and convert it. You must build lib with emcc also.

Upvotes: 0

Neil Roberts
Neil Roberts

Reputation: 469

The command you have described in the question is linking rather than compiling. However in general you should just be able to replace gcc with emcc and it will do the right thing. In this case you will need to replace not only this linking command but also the commands used to compile the sources to the .o files.

It would probably be a good idea to take out the -march option.

It looks like your project is using libsndfile and FFTW. You will probably need to compile these libraries yourself using emscripten. Both of them are using autotools so with a bit of luck you can compile them with emscripten simply by adding the following parameters when you run the configure script:

./configure --prefix=$HOME/emscripten-libs CC=emcc
make && make install

Then when you link your program you can specify -L$HOME/emscripten-libs/lib instead of -L/usr/local/lib.

Upvotes: 4

Related Questions