Showing posts with label U1709. Show all posts
Showing posts with label U1709. Show all posts

Thursday, October 25, 2018

Compiling C++ Codes into shared Library and Linking Them


Compiling C++ Codes into shared library and linking them

Let's say you have files print.h and print.cpp, shown below that contains C++ codes you wish to compile as .so (shared object) file and they are stored in ~/demo/lib folder.

#ifndef __PRINT__
#define __PRINT__

#include <iostream>
#include <stdio.h>
void print(const char* p, bool endl = true);

#endif
#include <print.h>

void print(const char* p, bool endl)
{
std::cout << "print: " << p;
if (endl) std::cout << std::endl;
}

You can compile the above code into .o (static library) file with the command

~/demo/g++ -o lib.o -c ./lib/print.cpp

But we want to compile it as .so (shared library) file so we use the command instead

~/demo/g++ -o ./lib/liblib.so -shared -fpic ./lib/print.cpp

Note that with the above commands, liblib.so and lib.o is created in ~/demo/lib folder upon successful compilation.

Now let's say you have another set of C++ files loop.cpp and loop.h, shown below and you wish to compile them together with another C++ file into an .so file...

#ifndef __LOOP__
#define __LOOP__

#include <iostream>
#include <unistd.h>

class loop
{
public:
loop()
{
std::cout << "loop constructor"<< std::endl;
}
virtual ~loop()
{
std::cout << "loop destructor"<< std::endl;
}

void run(int i, int r);
};

#endif

#include <loop.h>

void loop::run(int i, int r)
{
while(r)
{
sleep(i);
std::cout << "loop..." << std::endl;
r--;
}
}

To compile print.cpp and loop.cpp files into a single .so file, do the command as shown below

~/demo/g++ -o ./lib/liblib.so -shared -fpic ./lib/print.cpp ./lib/loop.cpp

Now that we have .so file already, let's create a C++ program that will use this shared library and use the function and class it contains.
The C++ file below will be our C++ program. It is stored as main.cpp in ~/demo folder.
It uses print() from print.cpp to print "hello world" and uses loop class to perform a loop of 5 intervals.

#include <iostream>
#include <print.h>
#include <loop.h>

int main()
{
print("hello world");
loop p;
p.run(1,5);
return 0;
}

Let's compile this code and link the shared library with it. The command below shows how to compile it into "run".

g++ -o run main.cpp -L/lib -llib -I./lib -I./

If compile is successful, and executable named 'run' should appear in ~/demo folder. If you execute it, you might encounter an error as shown below. This is because when executing a program that has dependency on a shared library, it needs to know where the library is.

run: error while loading shared libraries: liblib.so: cannot open shared object file: No such file or directory

To fix this, you can do either of the two possible solutions

1. Copy the liblib.so into any folder listed in the system's LD_LIBRARY_PATH environment variable. This is a more consistent solution because you only do this once.

2. add ~/demo/lib folder into LD_LIBRARY_PATH environment variable. You have to keep setting this every time you try to execute your program in a new terminal. To do this, follow the command below:

~/setenv LD_LIBRARY_PATH ~/demo/lib:${LD_LIBRARY_PATH}

Now try to run the program again.

Using Makefile 

You can create a makefile to simplify this process. Copy the code below into ~/demo/makefile

all: so main

so : ./lib/print.cpp ./lib/loop.cpp
g++ -o ./lib/liblib.so -shared -fpic ./lib/print.cpp ./lib/loop.cpp -I./lib

main : ./lib/liblib.so
g++ -o run main.cpp -L./lib -llib -I./lib -I ./

Then just call 'make' and the whole compiling process will be done.


Linking the Shared Library (.so) in Unison

In this guide, we will link a share library (.so) in a Unison test program and use the functions and classes defined in the shared library.

1. Optional: copy the .so file as well as its .h header files into test program folder

2. After loading test program, open TestTool



3. In the TestTool, select the application library you want to link the .so file. double-click it to open its settings. Below shows that I selected ST_DLOG application library from my test program.



4.  Click on "Compiler and Linker" tab. Click the "Add Path.." button and select the path where the .so file is located.



5. Once done, you will see the path in the "Linker Paths" list box.



6. In the "Linker Flags" list box, click an empty row. then type -L<.so path> -l<.so name w/o lib prefix>. The -l flag lets me specify the filename of the .so file I want to use, but I without the 'lib' prefix and .so extension. My .so file's filename is liblib.so so removing the prefix and extension, it becomes -llib.



7. Click "Include Paths" tab.



8. Click the "Add Path.." button and select the path where the .h file is selected. Once done, you will see the path in the "Include Paths" list box.



9. Add the include file in the application library's source code. Below shows I declared the include on ST_Datalog.cpp. I included the 2 header files from my shared library - loop.h and print.h



10. Somewhere in ST_Datalog.cpp, i called mydemo::print() function which is defined in the shared library.


11. Finally, compile your application library.










Tuesday, June 19, 2018

How To Compile CURI FAmodule in CentOS 7.2 with U1709 64-Bit OS and CURI 1.21

Note that this instruction is tested only with the following system and software versions:

OS: CentOS 7.2
Unison: U1709
Curi: CURI_1.12_2.121.x86_64

1. edit your project.makefile to look like this:

TARGET_MINOR_VERSION = 1

SO_TARGET = fademo_hooks.so

# -- Start of project files --
PROJECT_SOURCES = fademo_attach.cpp \
fademo_hooks.cpp  \

PROJECT_HEADERS   = fademo_hooks.h \

NO_PTHREAD=true
MBITS=64
PROJECT_INCLUDE_PATHS = -DLINUX_TARGET -DLINUX -DUNISON -I. -I/ltx/include 
PROJECT_LIBRARIES=-Wl,-rpath,/ltx/lib$(MBITS) -L/ltx/lib$(MBITS) -levxa

ifeq ("$(BUILD_OS)", "Linux")
CFLAGS:=-DLINUX_TARGET
endif

LDFLAGS:=

prepare:

Note the "MBITS=64" added

2. update all "long" arguments in fademo_hooks.h, fademo_attach.cpp, fademo_hooks.cpp to FA_LONG and "unsigned long" to FA_ULONG if any

3. use this command to compile > gmake CFG=Release MBITS=64 MBITS_CURI=64

4. after compiling as above, the .so files are stored in ~Release/ folder. rename Release to Release64 

5. update CURI config file /opt/ateTools/curi/unison/config/curi_conf.xml to point to this FAmodule:

<CURI_CONF>
    <Config Configuration_ID="DefaultConfiguration">
        <ConfigEnv Equipment_Path="/opt/ateTools/curi/unison/lib" Communications_Path="/opt/ateTools/curi/unison/lib" User_Path="/home/localuser/Desktop/famodule/Release"/>
        <CommunicationsList>
            <GPIB_IF>
                <Settings>
                    <stringSetting token="IbcAUTOPOLL" value="0"/>
                </Settings>
            </GPIB_IF>
            <RS232_IF/>
            <TTL_IF/>
            <TTL_IF DriverID="ASL PCI PORT" AccessName="ASL_XP_TTL" Library="curi_asl_ttl" />
            <TTL_IF DriverID="ASL AT PORT" AccessName="ASL_NT_TTL" Library="curi_asl_ttl" />
            <TCPIP_IF Port="65000"/>
            <USB_IF/>
        </CommunicationsList>
    </Config>

    <UserLibrary AccessName="Demo famodule" Library="fademo_hooks">
        <Settings>
    <stringSetting token="Module Type" value="FAPROC Module"/>
    <stringSetting token="Priority" value="50"/>
        </Settings>
    </UserLibrary>

Where /home/localuser/Desktop/famodule/Release is the path (without the '64' suffix) where your Famodule .so file is located and fademo_hooks is the name of the .so file

6. make sure to relaunch Unison

Sunday, January 21, 2018

GEM FAQ's


How to enable GEM in Unison/CentOS
  • SECS/Gem is initially disabled in Unison. To enable SECS/Gem in Unison, you will need to run the config_gem command from xterm. This command is found in the /ltx/com directory. 
  • config_gem <tester_name> [-enable|-disable] [-verbose] [-reset]
  • The execution of config_gem tool will create the gem_config file for CGem in the tester pwrup directory
    • /ltx/testers/<tester_name>/pwrup/gem_config

SECS-GEM on CentOS 7.2, U1709

  • make sure to yum install the latest version. the first U1709 version release has issue on GEM component and may require update so...