When using C ++ development programs or libraries, provide the libraries to others.
However, the user uses a C-developed program. When linking the library generated by G ++ compilation, it is different from the library generated by GCC.
The first is the static library. Take libmylib. A compiled by link g ++ as an example.
Mylib depends on the pthread, RT, and math libraries. The links must be placed after mylib.
At the same time,-wl, -- no-as-needed-LDL must be added at the end.
Arm and x86 are somewhat different, that is, arm's GCC does not automatically link to the math library. You need to manually add links.
1 CC=arm-linux-gnueabihf-gcc -std=c99 2 CCLDFLAGS= -L. -lmylib -lstdc++ -pthread -lrt -lm -Wl,--no-as-needed -ldl 3 #CC=gcc -m32 -std=c99 4 #CCLDFLAGS=-L. -lroot -lstdc++ -pthread -lrt -Wl,--no-as-needed -ldl 5 6 all:test 7 8 test.o:test.c 9 $(CC) -I../inc/ test.c -c -o test.o10 11 test:test.o libmylib.a12 $(CC) test.o -o test $(CCLDFLAGS)13 14 .PHONY: clean15 16 clean:17 -rm test test.o -f
Example of dynamic library generated by GCC link g ++:
Libmylib. So library. When g ++ is used for compiling, stdc ++ Library (-static-libstdc ++) is statically linked ).
1 CC=gcc -m32 -std=c99 2 #CC=arm-linux-gnueabihf-gcc -std=c99 3 CCLDFLAGS=-lstdc++ -L. -lmylib 4 5 Root:root 6 7 root.o:test.c 8 $(CC) -I../inc/ test.c -c -o root.o 9 10 root:root.o libmylib.so11 $(CC) root.o -o root $(CCLDFLAGS)12 13 14 .PHONY: clean15 16 clean:17 -rm root *.o
Makefile example of static library and dynamic library generated by GCC link g ++