emscripten-core / emscripten-core/emscripten

how to use emcc compiler to replace gcc

Open
#24,928 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

I have an existing Makefile that uses gcc as the compiler.
To compile a C program i would use the following:

SV_main.o : SV_main.c

$(CC) $(CFLAGS1) $(CFLAGS2) $(CFLAGS3) -c SV_main.c -o SV_main.o

where
1. CC=/usr/local/gcc14.2.0/bin/gcc -I/usr/local/gcc14.2.0/include/c++/14.2.0/ -I/usr/local/gcc14.2.0/include/
2. CFLAGS1=-Wall -Wfatal-errors -trigraphs
3. CFLAGS2=-I$(HOME)open62541 -I/usr/include/libxml2/ -I/usr/local/src/ADS/AdsLib/ -I/usr/include/ -I/usr/local/include/ -I/usr/include/python3.11 -I/usr/local/src/ptpd/src/ -I/usr/local/src/ptpd/src/dep/ -I/usr/local/src/include/ -I/usr/local/src/libwebsockets/build/
4. CFLAGS3=-pass-exit-codes -g -O

Then to link and produce the executable, i would use:

myNewServer: $(OBJ)
$(CPP) $(LDIR1) $(LDIR2) $(LDIR3) $(LDIR4) $(LDIR5) $(LDIR6) $(LDIR7) $(LDIR8) $(LDIR9) -o $@ $^ $(LIBS) $(LDFLAGS) $(LDIR1) -l:libldap.a -l:liblber.a -l:libfreeradius-client.a \
-l:liblely-coapp.a -l:liblely-io2.a -l:liblely-ev.a -l:liblely-co.a -l:liblely-can.a -l:liblely-util.a -l:liblely-libc.a -l:liblely-tap.a -l:liblely-util.a

where
1. CPP=/usr/local/gcc14.2.0/bin/g++
2. LDIR1=-L/usr/local/lib
3. LDIR2=-L/usr/lib
4. LDIR3=-L/usr/local/gcc14.2.0/lib/lib64
5. LDIR4=-L/usr/local/src/ADS/build
6. LDIR5=-L/usr/lib/python3.11/config-3.11-aarch64-linux-gnu
7. LDIR6=-L/usr/local/src/p-net-1.0.2-Linux-aarch64/lib
8. LDIR7=-L/usr/local/src/openldap/libraries/libldap/.libs # this is where libldap.a is located
9. LDIR8=-L/usr/local/src/openldap/libraries/liblber/.libs # this is where liblber.a is located
10. LDIR9=-L/usr/local/src/freeradius-client/lib/./libs # this is where libfreeradius-client.a is located
11. LDFLAGS=-L$(HOME)open62541/build/bin/ -l:libopen62541.a -l:liblibmdnsd.a

I prepared a sample C program as follows:

#ifdef no_almagamation
#include
#include
#include
#else
#include "open62541.h"
// no point #define here.. must set in open62541.h
#endif
#include
#include "SV_Emscripten.h"

char SoftwareVersion[MAX_STRING_SIZE];
char DataBlockVersion[MAX_STRING_SIZE];
char InstrumentTime[MAX_STRING_SIZE];
char MeasurementTime[MAX_STRING_SIZE];

emscripten getAirgardData(void);
char* EMS_getSoftwareVersion(void);
char* EMS_getDataBlockVersion(void);
char* EMS_getInstrumentTime(void);
char* EMS_getMeasurementTime(void);

char* EMS_getSoftwareVersion() {
return SoftwareVersion;}

char* EMS_getDataBlockVersion() {
return DataBlockVersion;}

char* EMS_getInstrumentTime() {
return InstrumentTime;}

char* EMS_getMeasurementTime() {
return MeasurementTime;}

emscripten getAirgardData()
{
UA_Client myUA_Client; // open62541 related
myUA_Client = UA_Client_new(); // open62541 related
// the intention is the use myUA_Client to connect to a backend UA_Server and then populate the variables.
// then the js in the html will use the get functions to read the variables and update the html page

emscripten myAirgardData;

// transfer the values in global variables into emscripten structure
strncpy(myAirgardData.SoftwareVersion, EMS_getSoftwareVersion(), MAX_STRING_SIZE);
strncpy(myAirgardData.DataBlockVersion, EMS_getDataBlockVersion(), MAX_STRING_SIZE);

strncpy(myAirgardData.InstrumentTime, EMS_getInstrumentTime(), MAX_STRING_SIZE);
strncpy(myAirgardData.MeasurementTime, EMS_getMeasurementTime(), MAX_STRING_SIZE);

return myAirgardData;
}

Now, to use emcc to compile the above sample C program, i use the following in the Makefile:

SV_Emscripten.js : SV_Emscripten.c
$(EMCC) $(CFLAGS1) $(CFLAGS2) $(CFLAGS3) SV_Emscripten.c -o SV_Emscripten.html -s'EXPORTED_FUNCTIONS=@/home/pi/OPCProject/mount-origin/emc_export.txt' -s'EXPORTED_RUNTIME_METHODS=["ccall", "cwrap"]' -sNO_DYNAMIC_EXECUTION=1

I get these errors and warning:

In file included from SV_Emscripten.c:6:
In file included from ./open62541.h:191:
/usr/include/stdint.h:26:10: fatal error: 'bits/libc-header-start.h' file not found
26 | #include

Note: I am using open62541 libraries and header file to compile my program.

Can somehow advise me how to resolve this error?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.