Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c++ compile (g++) fails #2

Open
pulmark opened this issue Sep 10, 2019 · 4 comments
Open

c++ compile (g++) fails #2

pulmark opened this issue Sep 10, 2019 · 4 comments

Comments

@pulmark
Copy link

pulmark commented Sep 10, 2019

Undefined references during linking and then some warnings. I don't understand why linkage fails, because header while has extern "C" linkage.

When I include the c file directly (#include fpc.c) in my project then it builds.

Below is the log of compile. I edited the make file to use C++ compiler and changed the std library option.

g++ -Wall -O2 -std=c++11 -DNDEBUG fpc.c cli.c -o fpc
fpc.c: In function ‘void byte_count(U8*, int, U32*, int)’:
fpc.c:412:14: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
412 | for(a = 0;a < (len & (~7));a += 8){
| ~~^~~~~~~~~~~~~~
fpc.c:431:9: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
431 | for(;a < len;)
| ~~^~~~~
fpc.c:433:14: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
433 | for(a = 0;a < sym_num;a++)
| ~~^~~~~~~~~
fpc.c: In function ‘void sort_inc(Fsym*, int)’:
fpc.c:442:14: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
442 | for(a = 0;a < num;a++){
| ~~^~~~~
fpc.c:460:14: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
460 | for(a = 0;a < num;a++){
| ~~^~~~~
fpc.c:466:14: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
466 | for(a = 0;a < num;a++){
| ~~^~~~~
fpc.c: In function ‘int construct_dec_table(U8*, Dnode*, int)’:
fpc.c:490:14: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
490 | for(a = 0;a < sym_num;a++){
| ~~^~~~~~~~~
fpc.c:505:14: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
505 | for(a = 0;a < sym_num;a++){
| ~~^~~~~~~~~
fpc.c: In function ‘void construct_enc_table(Enode*, Fsym*, int)’:
fpc.c:539:14: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
539 | for(a = 0;a < num;a++){
| ~~^~~~~
fpc.c:548:14: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
548 | for(a = 0;a < num;a++){
| ~~^~~~~
fpc.c: In function ‘U32 write_prefix_descr(Enode*, U8*, int)’:
fpc.c:676:14: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
676 | for(a = 0;a < sym_num;a++){
| ~~^~~~~~~~~
fpc.c:680:13: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
680 | while(a+1 < sym_num && previous == lookup[a+1].len)
| ~~~~^~~~~~~~~
fpc.c: In function ‘int FPC_compress_block(void*, const void*, int, int)’:
fpc.c:913:14: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
913 | for(a = 0;a < sym_num;a++)
| ~~^~~~~~~~~
fpc.c:924:14: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
924 | for(a = 0;a < sym_num && s[a].freq == 0;a++);
| ^~~~~~~~~
fpc.c:951:21: warning: comparison of integer expressions of different signedness: ‘U32’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
951 | if(compressed_size >= size){
| ~~~~~~~~~~~~~~~~^~~~~~~
fpc.c: In function ‘size_t comp_adaptive(void*, void*, size_t)’:
fpc.c:1038:41: warning: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Wsign-compare]
1038 | for(int c = 1;c <= MBLOCK && (cSTEP) <= inlen - a;c++){
| ~~~~~~~~~^~~~~~~~~~~~
fpc.c: In function ‘size_t FPC_compress(void
, void*, size_t, int)’:
fpc.c:77:23: warning: comparison of integer expressions of different signedness: ‘size_t’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
77 | #define MIN(A,B) ((A) < (B)?(A):(B))
| ~~~~^~~~~
fpc.c:1095:14: note: in expansion of macro ‘MIN’
1095 | U32 step = MIN(inlen,bsize);
| ^

/usr/lib64/gcc/x86_64-suse-linux/9/../../../../x86_64-suse-linux/bin/ld: /tmp/ccwLzlL2.o: in function dec_file(_IO_FILE*, _IO_FILE*) [clone .part.0]': cli.c:(.text+0xae): undefined reference to FPC_decompress_block'
/usr/lib64/gcc/x86_64-suse-linux/9/../../../../x86_64-suse-linux/bin/ld: /tmp/ccwLzlL2.o: in function comp_file(_IO_FILE*, _IO_FILE*, int)': cli.c:(.text+0x2cc): undefined reference to FPC_compress'
/usr/lib64/gcc/x86_64-suse-linux/9/../../../../x86_64-suse-linux/bin/ld: /tmp/ccwLzlL2.o: in function bench_file(_IO_FILE*, unsigned int, int)': cli.c:(.text+0x528): undefined reference to FPC_compress'
/usr/lib64/gcc/x86_64-suse-linux/9/../../../../x86_64-suse-linux/bin/ld: cli.c:(.text+0x543): undefined reference to FPC_compress' /usr/lib64/gcc/x86_64-suse-linux/9/../../../../x86_64-suse-linux/bin/ld: cli.c:(.text+0x55e): undefined reference to FPC_compress'
/usr/lib64/gcc/x86_64-suse-linux/9/../../../../x86_64-suse-linux/bin/ld: cli.c:(.text+0x577): undefined reference to FPC_compress' /usr/lib64/gcc/x86_64-suse-linux/9/../../../../x86_64-suse-linux/bin/ld: cli.c:(.text+0x5d5): undefined reference to FPC_decompress'
/usr/lib64/gcc/x86_64-suse-linux/9/../../../../x86_64-suse-linux/bin/ld: cli.c:(.text+0x5f0): undefined reference to FPC_decompress' /usr/lib64/gcc/x86_64-suse-linux/9/../../../../x86_64-suse-linux/bin/ld: cli.c:(.text+0x60b): undefined reference to FPC_decompress'
/usr/lib64/gcc/x86_64-suse-linux/9/../../../../x86_64-suse-linux/bin/ld: cli.c:(.text+0x624): undefined reference to `FPC_decompress'
collect2: error: ld returned 1 exit status
make: *** [Makefile:5: fpc] Error 1

@kagiannis
Copy link
Owner

kagiannis commented Sep 10, 2019

Hello

extern "C" is used when fpc.c is compiled as c but then included by a c++ application

To make it work you should either compile fpc.c seperately as c application

gcc -Wall -O2 -DNDEBUG -c fpc.c -o fpc.o 

and then link it with c++

g++ -Wall -O2 -DNDEBUG cli.c fpc.o -o fpc

or if you want to compile both with g++ you should remove extern "C"

@kagiannis
Copy link
Owner

Also you should notice that fpc.c uses variable length arrays that they are not present in standard c++. They are included only in standard c (C99 and above). So it can be compiled as c++ only with compilers that support variable length arrays as extension like g++ and clang++.

@pulmark
Copy link
Author

pulmark commented Sep 10, 2019

Thanks for the info. I made a static lib Makefile for fpc.c by using g++ and it works. Probably will use it instead of just using the #include "fpc.c" trick.

@pulmark
Copy link
Author

pulmark commented Sep 10, 2019

Makefile for libfpc.a:

CC = g++
TARGET = libfpc.a
CFLAGS= -pipe -fPIC -O3 -g -Wall -c -march=native -mtune=native

INC = -I./
OBJ_DIR=./obj
OUT_DIR=./
SRC_DIR=./

#Lib for fpc.c
FILES = $(SRC_DIR)/fpc.c
$(TARGET): $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(FILES))
ar -rcs -o $(OUT_DIR)/$@ $^

#Compiling every *.c to *.o
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c dirmake
$(CC) -c $(INC) $(CFLAGS) -o $@ $<

dirmake:
@mkdir -p $(OUT_DIR)
@mkdir -p $(OBJ_DIR)

clean:
rm -f $(OBJ_DIR)/*.o $(OUT_DIR)/$(TARGET)

rebuild: clean build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants