General engineering
New Project-> C Project
mysharedlibrary
/*
* libMysharedlibrary.h
*
* Created on: 2023年8月23日
* Author: yeqiang
*/
#ifndef LIBMYSHAREDLIBRARY_H_
#define LIBMYSHAREDLIBRARY_H_
extern void mysharedlibraryfunc ();
#endif /* LIBMYSHAREDLIBRARY_H_ */
/*
* mysharedlibrary.c
*
* Created on: 2023年8月23日
* Author: yeqiang
*/
#include <stdio.h>
#include "libMysharedlibrary.h"
void mysharedlibraryfunc (){
printf("mysharedlibrary func...\n");
}
build
13:33:10 **** Build of configuration Debug for project mysharedlibrary ****
make all
Building file: ../mysharedlibrary.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"mysharedlibrary.d" -MT"mysharedlibrary.o" -o "mysharedlibrary.o" "../mysharedlibrary.c"
Finished building: ../mysharedlibrary.c
Building target: libmysharedlibrary.so
Invoking: GCC C Linker
gcc -shared -o "libmysharedlibrary.so" ./mysharedlibrary.o
Finished building target: libmysharedlibrary.so
13:33:11 Build Finished. 0 errors, 0 warnings. (took 165ms)
New Project-> C Project
configure include
configure the library
source code
/*
============================================================================
Name : executableproj.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include "libMysharedlibrary.h"
int main(void) {
puts("Hello World"); /* prints Hello World */
mysharedlibraryfunc();
return EXIT_SUCCESS;
}
build
13:36:50 **** Build of configuration Debug for project executableproj ****
make all
Building file: ../src/executableproj.c
Invoking: GCC C Compiler
gcc -I/home/yeqiang/eclipse-workspace/mysharedlibrary -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/executableproj.d" -MT"src/executableproj.o" -o "src/executableproj.o" "../src/executableproj.c"
Finished building: ../src/executableproj.c
Building target: executableproj
Invoking: GCC C Linker
gcc -L/home/yeqiang/eclipse-workspace/mysharedlibrary/Debug -o "executableproj" ./src/executableproj.o -lmysharedlibrary
Finished building target: executableproj
13:36:50 Build Finished. 0 errors, 0 warnings. (took 166ms)
Execution, error: cannot open shared object file: No such file or directory
Reason: The libmysharedlibrary.so directory is not configured to /etc/ld.so.conf
Define the environment variable LD_LIBRARY_PATH at zero time and succeed.
Gnu AutoTools project
Makefile.am
bin_PROGRAMS=a.out
a_out_SOURCES=test.c
# Linker options for a.out
a_out_LDFLAGS = -L/home/yeqiang/eclipse-workspace/mysharedlibrary/Debug -lmysharedlibrary
# Compiler options for a.out
a_out_CPPFLAGS = -I/home/yeqiang/eclipse-workspace/mysharedlibrary
build 报错 undefined reference to `mysharedlibraryfunc'
13:43:16 **** Build of configuration Build (GNU) for project test ****
make all
Making all in src
make[1]: 进入目录“/home/yeqiang/eclipse-workspace/test/src”
gcc -DPACKAGE_NAME=\"test\" -DPACKAGE_TARNAME=\"test\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"test\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"test\" -DVERSION=\"1.0\" -I. -I/home/yeqiang/eclipse-workspace/mysharedlibrary -g -O2 -MT a_out-test.o -MD -MP -MF .deps/a_out-test.Tpo -c -o a_out-test.o `test -f 'test.c' || echo './'`test.c
mv -f .deps/a_out-test.Tpo .deps/a_out-test.Po
gcc -g -O2 -L/home/yeqiang/eclipse-workspace/mysharedlibrary/Debug -lmysharedlibrary -o a.out a_out-test.o
/usr/bin/ld: a_out-test.o: in function `main':
/home/yeqiang/eclipse-workspace/test/src/test.c:17: undefined reference to `mysharedlibraryfunc'
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:353:a.out] 错误 1
make[1]: 离开目录“/home/yeqiang/eclipse-workspace/test/src”
make: *** [Makefile:348:all-recursive] 错误 1
"make all" terminated with exit code 2. Build might be incomplete.
13:43:16 Build Failed. 4 errors, 0 warnings. (took 166ms)
After testing, the most basic dynamic library linking process
gcc -I/home/yeqiang/eclipse-workspace/mysharedlibrary -c -o "executableproj.o" "executableproj.c"
gcc -L/home/yeqiang/eclipse-workspace/mysharedlibrary/Debug -o "executableproj" executableproj.o -lmysharedlibrary
Can be executed successfully
After repeated testing, it was found that the successful operation process of manual execution is as follows
gcc -I/home/yeqiang/eclipse-workspace/mysharedlibrary -c -o a_out-test.o test.c
gcc -L/home/yeqiang/eclipse-workspace/mysharedlibrary/Debug -o a.out a_out-test.o -lmysharedlibrary
The failure process is as follows
gcc -I/home/yeqiang/eclipse-workspace/mysharedlibrary -c -o a_out-test.o test.c
gcc -L/home/yeqiang/eclipse-workspace/mysharedlibrary/Debug -lmysharedlibrary -o a.out a_out-test.o
That is: the root cause of undefined reference to `mysharedlibraryfunc' caused by Gnu Autotools Project linking dynamic library is that the -l parameter (-lmysharedlibrary) should be followed by the gcc command, not before the a_out-test.o parameter!
Makefile.in is automatically generated, and it will be overwritten every time it is compiled. It is impossible to modify the parameter position here, gcc bug?
After further analysis, it was found that clang 16 does not have this problem, is it a gcc bug?
Finally solved, using clang to compile
Modify src/Makefile.am, define the CC variable, and point to clang
bin_PROGRAMS=a.out
a_out_SOURCES=test.c
CC=/usr/local/bin/clang
# Linker options for a.out
AM_LDFLAGS = -L/home/yeqiang/eclipse-workspace/mysharedlibrary/Debug -lmysharedlibrary
# Compiler options for a.out
AM_CPPFLAGS = -I/home/yeqiang/eclipse-workspace/mysharedlibrary
compile
14:53:40 **** Build of configuration Build (GNU) for project test ****
make all
Making all in src
make[1]: 进入目录“/home/yeqiang/eclipse-workspace/test/src”
/usr/local/bin/clang -DPACKAGE_NAME=\"test\" -DPACKAGE_TARNAME=\"test\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"test\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"test\" -DVERSION=\"1.0\" -I. -I/home/yeqiang/eclipse-workspace/mysharedlibrary -g -O2 -MT test.o -MD -MP -MF .deps/test.Tpo -c -o test.o test.c
mv -f .deps/test.Tpo .deps/test.Po
/usr/local/bin/clang -g -O2 -L/home/yeqiang/eclipse-workspace/mysharedlibrary/Debug -lmysharedlibrary -o a.out test.o
make[1]: 离开目录“/home/yeqiang/eclipse-workspace/test/src”
make[1]: 进入目录“/home/yeqiang/eclipse-workspace/test”
make[1]: 对“all-am”无需做任何事。
make[1]: 离开目录“/home/yeqiang/eclipse-workspace/test”
14:53:40 Build Finished. 0 errors, 0 warnings. (took 265ms)
run