Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Do you know how to make shared object files (*.so)?
I try to compile two files (*.c and *.h) with gcc (on a Linux System: Raspbian Wheezy) into this special archive file (*.so), ready to be loaded in Java with JNA:

Files-Download (files of the folders utils,ap and common copied to folder src):
wpa_ctrl.c und wpa_ctrl.h from the archiv hostapd-2.5.tar.gz von w1.fi

GCC:
gcc -I./ -o wpa_ctrl.so -shared wpa_ctrl.c wpa_ctrl.h

JAVA:
Java
import com.sun.jna.Library;
import com.sun.jna.Native;
public class LoadLibrary
{
	Public interface LoadLibrary extends Library
	{
	Public void wpa_ctrl_open();
	Public void wpa_ctrl_request;
	Public void wpa_ctrl_open(String wpa_ctrl_path);
	Public String wpa_ctrl_request(String ping);
	}
Public String testLoadLibrary() throws IOEexception
	{
	String libraryPath = new File ("resources/wpa_ctrl/wpa_ctrl.so");
	Wpa_ctrl wpa_ctrl = (Wpa_ctrl) Native.loadLirbrary(libraryPath, Wpa_ctrl.class)
	
	String wpa_ctrl_path = "/var/run/wpa_supplicant";
	wpa_ctrl.wpa_ctrl_open(wpa_ctrl_path):
	Strinf answer = wpa_ctrl.wpa_ctrl_request("PING");
	return answer;
	}
}


Problem: I traced the error from Java (Link Error: no function found) back to an error coming from gcc:
When checking with: objdump -t wpa_ctrl.so
or with: objdump -T wpa_ctrl.so
you will see, that the symbol-table (command-table) contains no funtions from wpa_ctrl.c or wpa_ctrl.h.

What to do to resolve my problem?
Posted

1 solution

The header file wpa_ctrl.h must indicate which symbols (functions and variables) should be public. These are indicated by prefixing the declarations with the extern keyword. Declarations without this keyword are not added to the symbol table.

Example for wpa_ctrl_open:
C++
extern void wpa_ctrl_open(void);
 
Share this answer
 
Comments
TheRainer 11-Nov-15 15:46pm    
Thanks Jochen. Your hint pushed me closer to a solution. After a while I examined the code of wpa_ctrl.c with my ide. All Code was greyed out. That, because the preprocessor directives in the code (#ifdef <directivename>) deactivated its execution. Directly defining them before (#define <directivename>) solved the problem with finding the functions. Since more errors with these functions occured, I will do more debugging until I accept a solution or post one additively.
Jochen Arndt 12-Nov-15 2:56am    
The problem is that your files are only portions of a Linux project. Such projects often have a setup script that will check the environment and what is supported and generate files used later for building.

If you want to build your own libarary based on this project you should create a similar setup or at least a make script that behaves similar.

A good starting point is building hostapd on the Pi and check which files are generated and which definitions are set. Then you may use these settings for your project.

But all these require at least basic knowledge about the build process on Linux and the used make utilities to understand what is happening.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900