Problems including (Klein) library in Ioquake

Hi everyone,

I’m trying to include the Klein library in ioquake. I want to use it in code/qcommon/q_shared.c and code/qcommon/q_math.c. The goal of this is to replace some of the geometry functions and see if there are any performance benefits. I’m doing this for my bachelor thesis.

The Klein library consists of two files: klein.h and libklein_c.a

I’ve added the following code

code/qcommon/q_shared.h:

#include "../klein/klein.h"

In the Makefile I added the following lines at the top:

KLEIN_LIB?=./code/klein/libklein_c.a

In the Makfile I made the changed these lines:

$(B)/$(CLIENTBIN)$(FULLBINEXT): $(Q3OBJ) $(LIBSDLMAIN)
	$(echo_cmd) "LD $@"
	$(Q)$(CC) $(CLIENT_CFLAGS) $(CFLAGS) $(CLIENT_LDFLAGS) $(LDFLAGS) $(NOTSHLIBLDFLAGS) \
		-o $@ $(Q3OBJ) \
		$(LIBSDLMAIN) $(CLIENT_LIBS) $(LIBS)

...

$(B)/$(SERVERBIN)$(FULLBINEXT): $(Q3DOBJ)
	$(echo_cmd) "LD $@"
	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(NOTSHLIBLDFLAGS) -o $@ $(Q3DOBJ) $(LIBS)

To these lines respectively:

$(B)/$(CLIENTBIN)$(FULLBINEXT): $(Q3OBJ) $(LIBSDLMAIN)
	$(echo_cmd) "LD $@"
	$(Q)$(CXX) $(CLIENT_CFLAGS) $(CFLAGS) $(CLIENT_LDFLAGS) $(LDFLAGS) $(NOTSHLIBLDFLAGS) \
		-o $@ $(Q3OBJ) ${KLEIN_LIB} \
		$(LIBSDLMAIN) $(CLIENT_LIBS) $(LIBS)

...

$(B)/$(SERVERBIN)$(FULLBINEXT): $(Q3DOBJ)
	$(echo_cmd) "LD $@"
	$(Q)$(CXX) $(CFLAGS) $(LDFLAGS) $(NOTSHLIBLDFLAGS) -o $@ $(Q3DOBJ) ${KLEIN_LIB} $(LIBS)

When I run make, I get the following errors:

CGAME_Q3LCC code/cgame/cg_main.c
) in #if/#elsif/../klein/klein.h:6 code/cgame/../qcommon/q_shared.h:26 code/cgame/cg_local.h:23 code/cgame/cg_main.c:24 Bad operator (
cpp: code/cgame/../klein/klein.h:11 code/cgame/../qcommon/q_shared.h:26 code/cgame/cg_local.h:23 code/cgame/cg_main.c:24 Syntax error in #include
) in #if/#elsif/../klein/klein.h:166 code/cgame/../qcommon/q_shared.h:26 code/cgame/cg_local.h:23 code/cgame/cg_main.c:24 Bad operator (
cpp: code/cgame/../klein/klein.h:168 code/cgame/../qcommon/q_shared.h:26 code/cgame/cg_local.h:23 code/cgame/cg_main.c:24 No newline at end of file
CGAME_Q3LCC code/game/bg_misc.c
make[2]: *** [Makefile:2864: build/release-linux-x86_64/baseq3/cgame/cg_main.asm] Error 1
make[2]: *** Waiting for unfinished jobs....
) in #if/#elsif../klein/klein.h:6 code/game/../qcommon/q_shared.h:26 code/game/bg_misc.c:25 Bad operator (
cpp: code/game/../klein/klein.h:11 code/game/../qcommon/q_shared.h:26 code/game/bg_misc.c:25 Syntax error in #include
) in #if/#elsif../klein/klein.h:166 code/game/../qcommon/q_shared.h:26 code/game/bg_misc.c:25 Bad operator (
cpp: code/game/../klein/klein.h:168 code/game/../qcommon/q_shared.h:26 code/game/bg_misc.c:25 No newline at end of file
make[2]: *** [Makefile:2861: build/release-linux-x86_64/baseq3/cgame/bg_misc.asm] Error 1

The log above says that the errors are on lines 6, 11 and 166 of klein.h. Below you can see the relevant sections of klein.h:

1.  // File: klein.h
2.  // Purpose: Klein C interface, suitable for binding to other languages
3.
4.  #pragma once
5. 
6.  #if __cplusplus
7.  extern "C"
8.  {
9.  #endif
10.
11. #include <xmmintrin.h>
...
166. #if __cplusplus
167. }
168. #endif

Does anyone know what is going wrong and how to solve it?

There is another problem I’ve come across. To be able to use malloc in q_shared.c, I added #include <stdlib.h> in q_shared.h. When I compile, I get the error:

cpp: code/game/../qcommon/q_shared.h:26 code/game/bg_pmove.c:26 Could not find include file <stdlib.h>
cpp: code/game/../klein/klein.h:8 code/game/../qcommon/q_shared.h:27 code/game/bg_pmove.c:26 Syntax error in #include

Klein library github: GitHub - jeremyong/klein: P(R*_{3, 0, 1}) specialized SIMD Geometric Algebra Library

So far I have added a newline to the end of the klein.h file and removed the following to code section from klein.h:

6.  #if __cplusplus
7.  extern "C"
8.  {
9.  #endif

and

166. #if __cplusplus
167. }
168. #endif

This solves the Bad operator and No newline at end of file errors mentioned previously.
The diffcult error that is yet to be fixed is the following:

cpp: code/game/../klein/klein.h:11 code/game/../qcommon/q_shared.h:26 code/game/bg_misc.c:25 Syntax error in #include

This is an error relating to the 11. #include <xmmintrin.h> line of klein.h. I’ve checked what happens if I add #include <sdtio.h> to klein.h. This also gives the same Syntax error in #include error.

It seems that the custom Q3LCC compiler used in the build process doesn’t accept any include statements in my klein.h library file.

The problem is solved!

Q3vm does not support custom libraries and system libraries intentionally. To avoid Q3vm in the compilation process, the following flag can be used: make BUILD_GAME_QVM=0.

1 Like