I'm try to compile a code write in Vala using a .vapi of libgcrypt.
I installed the library in the system and i run a test in c. The code write in C compiles without errors:
I typed the following command to compile the C code:
gcc -o main main.c -l gcrypt
The code runs perfectly.
But, the following code write in Vala don't compile using the command:
valac --pkg gcrypt --Xcc=-lgcrypt main.vala
The main.vala is
using GCrypt;
public static int main(string[] args)
{
Hash h;
Hash.open(out h, Hash.Algorithm.MD5, Hash.Flag.SECURE);
return 0;
}
I don't know what i doing wrong.
EDITED:
Sorry for not post the error, but there is:
Loaded package /usr/share/vala-0.30/vapi/glib-2.0.vapi'
Loaded package /usr/share/vala-0.30/vapi/gobject-2.0.vapi'
Loaded package /usr/share/vala-0.30/vapi/gcrypt.vapi'
cc -o '/home/kyul/Documents/Codes/Vala/test_gcrypt/main' '/home/kyul/Documents/Codes/Vala/test_gcrypt/main.vala.c' -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -lgobject-2.0 -lglib-2.0 '-lgcrypt'
/home/kyul/Documents/Codes/Vala/test_gcrypt/main.vala.c: In function ‘_vala_main’:
/home/kyul/Documents/Codes/Vala/test_gcrypt/main.vala.c:22:38: error: ‘GCRY_MD_FLAGSECURE’ undeclared (first use in this function)
gcry_md_open (&_tmp0_, GCRY_MD_MD5, GCRY_MD_FLAGSECURE);
^
/home/kyul/Documents/Codes/Vala/test_gcrypt/main.vala.c:22:38: note: each undeclared identifier is reported only once for each function it appears in
error: cc exited with status 256
Compilation failed: 1 error(s), 0 warning(s)
Most likely You are missing the .vapi file.
$ wget https://raw.githubusercontent.com/nemequ/vala-extra-vapis/master/gcrypt.vapi
$ patch < gcrypt.patch
$ valac --vapidir=. --pkg gcrypt --Xcc=-lgcrypt main.vala
$ ./main
$ echo $?
0
I've never used gcrypt api from vala before and had to patch gcrypt.vapi for Your example to work, but the patch is trivial:
--- gcrypt.vapi
+++ gcrypt.vapi
## -619,7 +619,7 ##
public Error get_oid (uchar[] buffer);
}
- [CCode (cname = "enum gcry_md_flags", cprefix = "GCRY_MD_FLAG")]
+ [CCode (cname = "enum gcry_md_flags", cprefix = "GCRY_MD_FLAG_")]
public enum Flag {
SECURE,
HMAC
Update: concerning Your cipher errors, obviously the Cipher class is missing free_function mapping:
--- a/gcrypt.vapi 2016-02-22 21:40:34.458977385 +0200
+++ b/gcrypt.vapi 2016-02-22 21:39:59.662976449 +0200
## -560,7 +560,7 ##
CBC_MAC /* Enable CBC message auth. code (MAC). */
}
[Compact]
- [CCode (cname = "gcry_cipher_hd_t", lower_case_cprefix = "gcry_cipher_")]
+ [CCode (cname = "struct gcry_cipher_handle", lower_case_cprefix = "gcry_cipher_", free_function = "gcry_cipher_close")]
public class Cipher {
public static Error open (out Cipher cipher, Algorithm algo, Mode mode, Flag flags);
public void close ();
Related
I'm trying to learn how to build custom linux images with Yocto and I'm struggling to create an image with both a shared library and a program that uses it.
I started by following this tutorial and everything went ok. Then I tried to separate the program from the library in two different layers, without success.
I started with the library code:
greetings.c
#include <stdio.h>
#include <string.h>
#include "greetings.h"
void get_greeting(char * buffer) {
if(buffer == NULL) {
return;
}
char greeting[] = "Hello world from the greetings lib\n";
strcpy(buffer, greeting);
return;
}
greetings.h
void get_greeting(char * buffer);
Makefile.am
AUTOMAKE_OPTIONS = foreign
lib_LTLIBRARIES = libgreetings.la
libgreetings_la_SOURCES = greetings.c
include_HEADERS = greetings.h
libgreetings_la_CFLAGS = -Wall -Werror -fPIC
libgreetings_la_LDFLAGS = -shared
ACLOCAL_AMFLAGS = -I m4
configure.ac
AC_INIT([Greetings lib], 1.0)
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_CONFIG_MACRO_DIR([m4])
LT_INIT()
AC_CONFIG_FILES(Makefile)
AC_OUTPUT
I added this code to a git repository and created a "meta-greetings" layer with the layer.conf and recipe files:
layer.conf
# We have a conf and classes directory, add to BBPATH
BBPATH .= ":${LAYERDIR}"
# We have recipes-* directories, add to BBFILES
BBFILES += "${LAYERDIR}/recipes-*/*/*.bb \
${LAYERDIR}/recipes-*/*/*.bbappend"
BBFILE_COLLECTIONS += "meta-greetings"
BBFILE_PATTERN_meta-greetings = "^${LAYERDIR}/"
BBFILE_PRIORITY_meta-greetings = "6"
LAYERDEPENDS_meta-greetings = "core"
LAYERSERIES_COMPAT_meta-greetings = "thud"
IMAGE_INSTALL_append = " greetings"
recipes-greetings/greetings/greetings_0.1.bb
SUMMARY = "bitbake-layers recipe"
DESCRIPTION = "Simple helloworld lib"
DEPENDS = ""
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://LICENSE;md5=96af5705d6f64a88e035781ef00e98a8"
KBRANCH = "master"
SRCREV = "1a908a8f8616af704ce71d693e88c6d4498f24c4"
SRC_URI = "git://bitbucket.org/Grifo/greetings_lib.git;branch=${KBRANCH};protocol=ssh"
S = "${WORKDIR}/git"
inherit autotools
So far so good, I added this layer to my bblayers file and proceeded to compile the final image. I run it in qemu and even got to see the files in /usr/lib:
However, there's no "libgreetings.so". I don't know if that may be the cause of the problem (still to explain) but the previously mentioned tutorial got similar results so I proceeded.
After that I did the program:
helloworld.c
#include <stdio.h>
#include "greetings.h"
int main(void) {
char greeting[40];
get_greeting(greeting);
printf("Hello world!\n");
printf("%s", greeting);
return 0;
}
Makefile.am
AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = hello_world
hello_world_SOURCES = helloworld.c
hello_world_LDADD = $(libdir)/libgreetings.so
ACLOCAL_AMFLAGS = -I m4
configure.ac
AC_INIT([Hello world], 1.0)
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_FILES(Makefile)
AC_OUTPUT
Added this code to git and created a "meta-helloworld" layer with the files:
layer.conf
# We have a conf and classes directory, add to BBPATH
BBPATH .= ":${LAYERDIR}"
# We have recipes-* directories, add to BBFILES
BBFILES += "${LAYERDIR}/recipes-*/*/*.bb \
${LAYERDIR}/recipes-*/*/*.bbappend"
BBFILE_COLLECTIONS += "meta-helloworld"
BBFILE_PATTERN_meta-helloworld = "^${LAYERDIR}/"
BBFILE_PRIORITY_meta-helloworld = "7"
LAYERDEPENDS_meta-helloworld = "core meta-greetings"
LAYERSERIES_COMPAT_meta-helloworld = "thud"
IMAGE_INSTALL_append = " helloworld"
recipes-helloworld/helloworld/helloworld_0.1.bb
SUMMARY = "bitbake-layers helloworld"
DESCRIPTION = "Simple helloworld program"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://LICENSE;md5=96af5705d6f64a88e035781ef00e98a8"
KBRANCH = "master"
SRCREV = "6a29425473286028e85e74003f2f57ecaf766354"
SRC_URI = "git://bitbucket.org/Grifo/hello_world.git;branch=${KBRANCH};protocol=ssh"
DEPENDS = "greetings"
S = "${WORKDIR}/git"
inherit autotools
After all this I bitbaked the final image again but got the following error:
(...)
Sstate summary: Wanted 7 Found 0 Missed 7 Current 737 (0% match, 99% complete)
NOTE: Executing SetScene Tasks
NOTE: Executing RunQueue Tasks
ERROR: helloworld-0.1-r0 do_compile: oe_runmake failed
ERROR: helloworld-0.1-r0 do_compile: Function failed: do_compile (log file is located at /var/tmp/workspaces/grifo/poky/build/tmp/work/armv5e-poky-linux-gnueabi/helloworld/0.1-r0/temp/log.do_compile.12040)
ERROR: Logfile of failure stored in: /var/tmp/workspaces/grifo/poky/build/tmp/work/armv5e-poky-linux-gnueabi/helloworld/0.1-r0/temp/log.do_compile.12040
Log data follows:
| DEBUG: SITE files ['endian-little', 'bit-32', 'arm-common', 'arm-32', 'common-linux', 'common-glibc', 'arm-linux', 'arm-linux-gnueabi', 'common']
| DEBUG: Executing shell function do_compile
| NOTE: make -j 8
| make: *** No rule to make target `/usr/lib/libgreetings.so', needed by `hello_world'. Stop.
| ERROR: oe_runmake failed
| WARNING: /var/tmp/workspaces/grifo/poky/build/tmp/work/armv5e-poky-linux-gnueabi/helloworld/0.1-r0/temp/run.do_compile.12040:1 exit 1 from 'exit 1'
| ERROR: Function failed: do_compile (log file is located at /var/tmp/workspaces/grifo/poky/build/tmp/work/armv5e-poky-linux-gnueabi/helloworld/0.1-r0/temp/log.do_compile.12040)
ERROR: Task (/var/tmp/workspaces/grifo/poky/meta-helloworld/recipes-helloworld/helloworld/helloworld_0.1.bb:do_compile) failed with exit code '1'
NOTE: Tasks Summary: Attempted 1966 tasks of which 1965 didn't need to be rerun and 1 failed.
Summary: 1 task failed:
/var/tmp/workspaces/grifo/poky/meta-helloworld/recipes-helloworld/helloworld/helloworld_0.1.bb:do_compile
Summary: There was 1 WARNING message shown.
Summary: There were 2 ERROR messages shown, returning a non-zero exit code.
I am sorry for the really long question but I felt like I need to give all the details since I don't know if the problem comes from my recipe or my autotools files.
Before I built the recipes and compile it using yocto, I first compiled and run it in my host computer using the shell and everything run fine. I compiled and make install the greetings library (/usr/local/lib) and after that compiled the helloworld program which run without any problem.
I know that I could probably do this easily all within the same layer, however I'm trying to do it in separate layers to simulate different projects. Another requirement of mine is to use autotools instead of cmake.
Thank you in advance,
Grifo
EDIT:
I got it to work! Thank you Alexander Kanavin for pointing me in the right direction. I just had to change hello_world_LDADD = $(libdir)/libgreetings.so to hello_world_LDADD = -lgreetings in my helloworld's Makefile.am.
libgreetings.so is a file needed only for development and so it does not get installed to the image (unless you also install libgreetings-dev package - that's where it went).
During cross-compile, you typically specify libraries to link with like this:
-lgreetings
So change hello_world_LDADD = $(libdir)/libgreetings.so to hello_world_LDADD = -lgreetings.
I would start with that. Typically you shouldn't hardcode them like that in the makefile, but rather 'discover' and check the library in configure.ac (e.g. using pkg-config, assuming your library installs the corresponding .pc file), and set the appropriate compiler and linker flags:
PKG_CHECK_MODULES(GREETINGS, [greetings])
Then, in Makefile.am:
hello_world_LDADD = $(GREETINGS_LIBS)
I am using QtCreator 4.2.1 with MinGW4.9.2 32 bit compiler (Win 8.1 platform).
My pgi project only has a main.cpp which uses objects and functions belonging to my PhygenicLib static library. This static library in turn relies on functions and classes and constants defined in the Mathlib2 static library along with the eigen3 template library (template-based, only headers, no .cpp!) found on the web.
Here is the PhygenicLib.pro which compiles nicely into the libPhygenicLib.a file (debug mode):
QT -= gui
TARGET = PhygenicLib
TEMPLATE = lib
CONFIG += staticlib
DEFINES += QT_DEPRECATED_WARNINGS
INCLUDEPATH += d:/Qt-apps/Mathlib2 d:/Qt-apps/eigen3
SOURCES += phygenic.cpp \ cinemeca_pg.cpp \ mecagen.cpp \
rc_perf_pg.cpp \ xploit_pg.cpp \ utilities.cpp \
Analex.cpp \ Anasyntax.cpp
HEADERS += phygenic.h \ cinemeca_pg.h \ mecagen.h \ rc_perf_pg.h \
xploit_pg.h \ utilities.h \ Analex.h \ Anasyntax.h
unix { target.path = /usr/lib
INSTALLS += target }
Here is the pgi.pro (debug mode):
QT += core
QT -= gui
CONFIG += c++11
TARGET = pgi
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
LIBS += d:/Qt-apps/build-Mathlib2-Desktop_Qt_5_5_1_MinGW_32bit-Release/release/libMathlib2.a \
d:/Qt-apps/build-PhygenicLib-Desktop_Qt_5_5_1_MinGW_32bit-Debug/debug/libPhygenicLib.a
INCLUDEPATH += d:/Qt-apps/PhygenicLib \
d:/Qt-apps/Mathlib2 \
d:/Qt-apps/eigen3
SOURCES += main.cpp
HEADERS +=
DEFINES += QT_DEPRECATED_WARNINGS
Note that Mathlib2 is compiled in release mode not debug mode, but that should not be a problem (?). Now, here is my main.cpp:
#include <iostream>
#include "Anasyntax.h"
using namespace std;
int main(int, char**)
{
ofstream fdmp("test"+extFichier[DMP]);
try { // bloc try
fdmp<<"--- test généraux interpréteur ---"<<endl ;
Anasyntax asynt ;
asynt.interprete(fdmp);
} catch (std::exception const& err) {
fdmp << err.what()<< endl<<" - Erreur fatale." << endl ;
cerr << err.what()<< endl<<"- Erreur fatale." << endl ;
}
cout<<'\a' ;
fdmp << "--- FIN DE L'EXECUTION --- "<< std::endl;
fdmp.close() ;
return EXIT_SUCCESS ;
}
#include Anasyntax.h is pulling all the headers from the various libraries wherever required (root of the include tree, so to say).
Now when I try to compile and link the project, I get the following diagnostic (sorry the language, QtCreator installed in French):
14:48:12: Exécution des étapes pour le projet pgi...
14:48:12: Débute : "C:\Qt\Tools\mingw492_32\bin\mingw32-make.exe" clean
C:/Qt/Tools/mingw492_32/bin/mingw32-make -f Makefile.Debug clean
mingw32-make[1]: Entering directory 'D:/Qt-apps/build-pgi-Desktop_Qt_5_5_1_MinGW_32bit-Debug'
del debug\main.o
Impossible de trouver D:\Qt-apps\build-pgi-Desktop_Qt_5_5_1_MinGW_32bit-Debug\debug\main.o
mingw32-make[1]: Leaving directory 'D:/Qt-apps/build-pgi-Desktop_Qt_5_5_1_MinGW_32bit-Debug'
C:/Qt/Tools/mingw492_32/bin/mingw32-make -f Makefile.Release clean
mingw32-make[1]: Entering directory 'D:/Qt-apps/build-pgi-Desktop_Qt_5_5_1_MinGW_32bit-Debug'
del release\main.o
Impossible de trouver D:\Qt-apps\build-pgi-Desktop_Qt_5_5_1_MinGW_32bit-Debug\release\main.o
mingw32-make[1]: Leaving directory 'D:/Qt-apps/build-pgi-Desktop_Qt_5_5_1_MinGW_32bit-Debug'
14:48:13: Le processus "C:\Qt\Tools\mingw492_32\bin\mingw32-make.exe" s'est terminé normalement.
14:48:13: Configuration inchangée, étape qmake sautée.
14:48:13: Débute : "C:\Qt\Tools\mingw492_32\bin\mingw32-make.exe"
C:/Qt/Tools/mingw492_32/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'D:/Qt-apps/build-pgi-Desktop_Qt_5_5_1_MinGW_32bit-Debug'
g++ -c -pipe -fno-keep-inline-dllexport -g -std=c++0x -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_CORE_LIB -I..\pgi -I. -Id:\Qt-apps\PhygenicLib -Id:\Qt-apps\Mathlib2 -Id:\Qt-apps\eigen3 -IC:\Qt\5.5\mingw492_32\include -IC:\Qt\5.5\mingw492_32\include\QtCore -Idebug -IC:\Qt\5.5\mingw492_32\mkspecs\win32-g++ -o debug\main.o ..\pgi\main.cpp
g++ -Wl,-subsystem,console -mthreads -o debug\pgi.exe debug/main.o d:/Qt-apps/build-Mathlib2-Desktop_Qt_5_5_1_MinGW_32bit-Release/release/libMathlib2.a d:/Qt-apps/build-PhygenicLib-Desktop_Qt_5_5_1_MinGW_32bit-Debug/debug/libPhygenicLib.a -LC:/Qt/5.5/mingw492_32/lib -lQt5Cored
d:/Qt-apps/build-PhygenicLib-Desktop_Qt_5_5_1_MinGW_32bit-Debug/debug/libPhygenicLib.a(mecagen.o): In function `ZN10Solide_dyn4calcEv':
D:\Qt-apps\build-PhygenicLib-Desktop_Qt_5_5_1_MinGW_32bit-Debug/../PhygenicLib/mecagen.cpp:220: undefined reference to `rotation_WM(V3)'
D:\Qt-apps\build-PhygenicLib-Desktop_Qt_5_5_1_MinGW_32bit-Debug/../PhygenicLib/mecagen.cpp:221: undefined reference to `Mat_transp_3x3(Mat3x3)'
and so on, and so on...
It looks like g++ is trying to link mecagen.cpp (which is already compiled as part of libPhygenicLib.a) and not finding references to symbols which are part of Mathlib2 library (includepath= d:/Qt-apps/Mathlib2, LIB= d:/.../release/libMathlib2.a).
I found a walk-around solution: just inserting in the main.cpp a reference to any Mathlib2 object or function, such as function coupure hereafter, enables to build and run the project executable without any warning/error:
int main(int, char**)
{
ofstream fdmp("test"+extFichier[DMP]);
coupure(0.,0.,0.) ; // any object or function from Mathlib2 works as well!
try { // etc...
So the problem is overcome but it drives me nuts not to understand why it won't work without inserting a useless code line... I have to say I was too lazy to ever learn about g++, make, qmake and makeFile sort of things; just pushing QtCreator buttons is so easy and comfortable...
I read again some posts with somewhat similar issues and found one comment regarding the order of declaring static libraries which may be critical in some cases.
And indeed, if I declared the LIBS in my pgi.pro in the reverse order, as follows:
LIBS += d:/Qt-apps/build-PhygenicLib-Desktop_Qt_5_5_1_MinGW_32bit-Debug/debug/libPhygenicLib.a \
d:/Qt-apps/build-Mathlib2-Desktop_Qt_5_5_1_MinGW_32bit-Release/release/libMathlib2.a
the project then compiles and links fine without any extraneous reference to a Mathlib2 object or function in the main.cpp. I got the lesson and will now take care of declaring LIBS in the appropriate order...
I tried to write a simple D Program and use it to access a simple C library but there is unknown error.
My c Code, Box.c
#include "Box.h"
int SayHello( int _int)
{
_int ++;
return _int;
}
My c header file, Box.h
#ifndef BOX_H_INCLUDED
#define BOX_H_INCLUDED
/* export interfaces */
#ifdef __cplusplus
extern "C" {
#endif
int SayHello( int _int);
#ifdef __cplusplus
}
#endif
#endif // BOX_H_INCLUDED
I compile it
gcc -c Box.c Box.h
resulting files
Box.o
Box.h.gch
I place them to my D Program's project directory
My D Code
module main;
import std.stdio;
import std.conv;
import std.c.stdio;
import clib;
int main(string[] args)
{
// test external c library
auto s = to!string( SayHello(3) ) ;
writefln( "my int is "~ s );
readln();
return 0;
}
My D interface file ( clib ), trying to link to my C library
module clib;
import std.c.stdio;
extern (C) int SayHello( int _int);
The error I get when I compile it using codeblocks
Compiling: hello.d
Linking console executable: bin/Debug/tutorial03-access-c-library4
obj/Debug/hello.o: In function `_Dmain':
/home/hamilton/Tutorial/tutorial03-access-c-library4/hello.d:11: **undefined reference to `SayHello'**
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings
Error is "undefined reference to `SayHello'"
There is no error I get when I compile it using command in console
$ dmd Box.o hello.d clib.di
it will be very painful if I cannot use codeblocks as I need the debugging functionality.
Thanks
Update:
Compiler setting in codeblocks as followed
Linker for dynamic libs: gcc -m32 -lrt
Linker for static libs: ar
Debugger: gdb
You can change the build options in CodeBlocks from Project -> Build Options, Compiler settings -> Other options. The simplest thing to do would be to just add Box.o to Other options.
I'm trying to compile reduced clang "driver.cpp" - added printf for arguments - and i'm getting linking with libLLVMSupport.a error - no StringSaver class found.
# lib
echo "compiling cmd builder (x86_64) ..."
clang++ `../llvm_34_local_installed2/bin/llvm-config --cxxflags` -I ../llvm_34/tools/clang/include -I ../llvm_34_build_local2/tools/clang/include -c ./lib_cmd_builder.cpp -o ./lib_cmd_builder_x86_64.o
echo "linking cmd builder (x86_64) ..."
CMD="clang++ `../llvm_34_local_installed2/bin/llvm-config --ldflags` ./lib_cmd_builder_x86_64.o -o exe_cmd_builder_x86_64 `../llvm_34_local_installed2/bin/llvm-config --libs` -lclangARCMigrate -lclangAST -lclangASTMatchers -lclangAnalysis -lclangApplyReplacements -lclangBasic -lclangCodeGen -lclangDriver -lclangDynamicASTMatchers -lclangEdit -lclangFormat -lclangFrontend -lclangFrontendTool -lclangIndex -lclangLex -lclangParse -lclangQuery -lclangRewriteCore -lclangRewriteFrontend -lclangSema -lclangSerialization -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangStaticAnalyzerFrontend -lclangTidy -lclangTidyGoogleModule -lclangTidyLLVMModule"
echo "[${CMD}]"
${CMD}
Error message:
Undefined symbols for architecture x86_64:
"typeinfo for llvm::cl::StringSaver", referenced from:
typeinfo for (anonymous namespace)::StringSetSaver in lib_cmd_builder_x86_64.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Checking libLLVM is found and contains CommandLine object data:
(found on -L path):
find ../llvm_34_local_installed2/lib/ -name "*upport*"
../llvm_34_local_installed2/lib//libLLVMSupport.a
(contains CommandLine.cpp object data):
ar -t ../llvm_34_local_installed2/lib//libLLVMSupport.a | grep CommandLine
CommandLine.cpp.o
The class StringSaver is declared in CommandLine.cpp:
/Users/asmirnov/Documents/dev/src/llvm_34/include/llvm/Support/CommandLine.h
class StringSaver {
virtual void anchor();
public:
virtual const char *SaveString(const char *Str) = 0;
virtual ~StringSaver() {}; // Pacify -Wnon-virtual-dtor.
};
StringSaver inheritor is declared and implemented in self file (lib_cmd_builder.cpp):
namespace {
class StringSetSaver : public llvm::cl::StringSaver {
public:
StringSetSaver(std::set<std::string> &Storage) : Storage(Storage) {}
const char *SaveString(const char *Str) LLVM_OVERRIDE {
return SaveStringInSet(Storage, Str);
}
private:
std::set<std::string> &Storage;
};
}
i've also checked llvm-config output and it seems to be ok (contain -L and -lLLVMSupport):
clang++ -L/Users/asmirnov/Documents/dev/src/llvm_34_local_installed2/lib -lcurses -lpthread -lz ./lib_cmd_builder_x86_64.o -o exe_cmd_builder_x86_64 -lLLVMInstrumentation -lLLVMIRReader -lLLVMAsmParser -lLLVMDebugInfo -lLLVMOption -lLLVMLTO -lLLVMLinker -lLLVMipo -lLLVMVectorize -lLLVMBitWriter -lLLVMBitReader -lLLVMTableGen -lLLVMXCoreDisassembler -lLLVMXCoreCodeGen -lLLVMXCoreDesc -lLLVMXCoreInfo -lLLVMXCoreAsmPrinter -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMX86Desc -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMSystemZDisassembler -lLLVMSystemZCodeGen -lLLVMSystemZAsmParser -lLLVMSystemZDesc -lLLVMSystemZInfo -lLLVMSystemZAsmPrinter -lLLVMSparcCodeGen -lLLVMSparcDesc -lLLVMSparcInfo -lLLVMR600CodeGen -lLLVMR600Desc -lLLVMR600Info -lLLVMR600AsmPrinter -lLLVMPowerPCCodeGen -lLLVMPowerPCAsmParser -lLLVMPowerPCDesc -lLLVMPowerPCInfo -lLLVMPowerPCAsmPrinter -lLLVMNVPTXCodeGen -lLLVMNVPTXDesc -lLLVMNVPTXInfo -lLLVMNVPTXAsmPrinter -lLLVMMSP430CodeGen -lLLVMMSP430Desc -lLLVMMSP430Info -lLLVMMSP430AsmPrinter -lLLVMMipsDisassembler -lLLVMMipsCodeGen -lLLVMMipsAsmParser -lLLVMMipsDesc -lLLVMMipsInfo -lLLVMMipsAsmPrinter -lLLVMHexagonCodeGen -lLLVMHexagonAsmPrinter -lLLVMHexagonDesc -lLLVMHexagonInfo -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo -lLLVMARMDisassembler -lLLVMARMCodeGen -lLLVMARMAsmParser -lLLVMARMDesc -lLLVMARMInfo -lLLVMARMAsmPrinter -lLLVMAArch64Disassembler -lLLVMAArch64CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMAArch64AsmParser -lLLVMAArch64Desc -lLLVMAArch64Info -lLLVMAArch64AsmPrinter -lLLVMAArch64Utils -lLLVMMCDisassembler -lLLVMMCParser -lLLVMInterpreter -lLLVMMCJIT -lLLVMJIT -lLLVMCodeGen -lLLVMObjCARCOpts -lLLVMScalarOpts -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMRuntimeDyld -lLLVMExecutionEngine -lLLVMTarget -lLLVMMC -lLLVMObject -lLLVMCore -lLLVMSupport -lclangARCMigrate -lclangAST -lclangASTMatchers -lclangAnalysis -lclangApplyReplacements -lclangBasic -lclangCodeGen -lclangDriver -lclangDynamicASTMatchers -lclangEdit -lclangFormat -lclangFrontend -lclangFrontendTool -lclangIndex -lclangLex -lclangParse -lclangQuery -lclangRewriteCore -lclangRewriteFrontend -lclangSema -lclangSerialization -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangStaticAnalyzerFrontend -lclangTidy -lclangTidyGoogleModule -lclangTidyLLVMModule
Also checked it's build for x86_64 (my host device):
lipo -info ../llvm_34_local_installed2/lib/libLLVMSupport.a
input file ../llvm_34_local_installed2/lib/libLLVMSupport.a is not a fat file
Non-fat file: ../llvm_34_local_installed2/lib/libLLVMSupport.a is architecture: x86_64
I had to use -fno-rtti while compiling library sources
I am a unskilled programmer and new to linux, I run into a problem when complining. I have two files 'ex_addinst.c' and 'lindo.h' in the same folder, I input command :
g++ -c ex_addinst.c
then, a object file ex_addinst.o is genetated with a warning:
ex_addinst.c: In function ‘int main()’:
ex_addinst.c:80: warning: deprecated conversion from string constant to ‘char*’
then I leak them with
g++ -Wall -o ex_addinst ex_addinst.o
and get the following info:
ex_addinst.o: In function `main':
ex_addinst.c:(.text+0x2b): undefined reference to `LSloadLicenseString'
ex_addinst.c:(.text+0x75): undefined reference to `LSgetVersionInfo'
ex_addinst.c:(.text+0xae): undefined reference to `LScreateEnv'
ex_addinst.c:(.text+0x10a): undefined reference to `LSgetErrorMessage'
...
...
ex_addinst.c:(.text+0x1163): undefined reference to `LSdeleteEnv'
collect2: ld returned 1 exit status
I guess that the header file 'lindo.h' is not complied into the .o file, but I have no idea what to do now. I have tried gcc, but get the same error. the version of my g++ and gcc is 4.4.5. I am using Ubuntu 10.10.
All the functions and structures have been declared in 'lindo.h'.
part of ex_addinst.c is as follows:
#include <stdio.h>
#include <stdlib.h>
/* LINDO API header file */
#include "lindo.h"
enter code here
int CALLTYPE LSwriteMPIFile(pLSmodel pModel,
char *pszFname);
/* Define a macro to declare variables for
error checking */
#define APIERRORSETUP \
int nErrorCode; \
char cErrorMessage[LS_MAX_ERROR_MESSAGE_LENGTH] \
/* Define a macro to do our error checking */
#define APIERRORCHECK \
if (nErrorCode) \
{ \
if ( pEnv) \
{ \
LSgetErrorMessage( pEnv, nErrorCode, \
cErrorMessage); \
printf("nErrorCode=%d: %s\n", nErrorCode, \
cErrorMessage); \
} else {\
printf( "Fatal Error\n"); \
} \
exit(1); \
} \
#define APIVERSION \
{\
char szVersion[255], szBuild[255];\
LSgetVersionInfo(szVersion,szBuild);\
printf("\nLINDO API Version %s built on %s\n",szVersion,szBuild);\
}\
/* main entry point */
int main()
{
APIERRORSETUP;
pLSenv pEnv;
pLSmodel pModel;
char MY_LICENSE_KEY[1024];
/*****************************************************************
* Step 1: Create a model in the environment.
*****************************************************************/
nErrorCode = LSloadLicenseString("home/li/work/tools/lindo/lindoapi/license/lndapi60.lic", MY_LICENSE_KEY);
if ( nErrorCode != LSERR_NO_ERROR)
{
printf( "Failed to load license key (error %d)\n",nErrorCode);
exit( 1);
}
......
......
......
APIERRORCHECK;
{
int nStatus;
double objval=0.0, primal[100];
/* Get the optimization result */
nErrorCode = LSgetInfo(pModel, LS_DINFO_GOP_OBJ, &objval);
APIERRORCHECK;
LSgetMIPPrimalSolution( pModel, primal) ;
APIERRORCHECK;
printf("\n\nObjective = %f \n",objval);
printf("x[0] = %f \n",primal[0]);
printf("x[1] = %f \n",primal[1]);
/* Get the linearity of the solved model */
nErrorCode = LSgetInfo (pModel, LS_IINFO_GOP_STATUS, &nStatus);
APIERRORCHECK;
/* Report the status of solution */
if (nStatus==LS_STATUS_OPTIMAL || nStatus==LS_STATUS_BASIC_OPTIMAL)
printf("\nSolution Status: Globally Optimal\n");
else if (nStatus==LS_STATUS_LOCAL_OPTIMAL)
printf("\nSolution Status: Locally Optimal\n\n");
else if (nStatus==LS_STATUS_INFEASIBLE)
printf("\nSolution Status: Infeasible\n\n");
}
/* >>> Step 7 <<< Delete the LINDO environment */
LSdeleteEnv(&pEnv);
/* Wait until user presses the Enter key */
printf("Press <Enter> ...");
getchar();
}
part of 'lindo.h' is:
/*********************************************************************
* Structure Creation and Deletion Routines (4) *
*********************************************************************/
pLSenv CALLTYPE LScreateEnv(int *pnErrorcode,
char *pszPassword);
pLSmodel CALLTYPE LScreateModel(pLSenv pEnv,
int *pnErrorcode);
int CALLTYPE LSdeleteEnv(pLSenv *pEnv);
int CALLTYPE LSdeleteModel(pLSmodel *pModel);
int CALLTYPE LSloadLicenseString(char *pszFname, char *pachLicense);
void CALLTYPE LSgetVersionInfo(char *pachVernum, char *pachBuildDate);
Thank you!
Thank you guys answering my problem. As you suggested, I need to link the library when complining. I have gotten the executable file with:
gcc -o ex_addinst ./ex_addinst.o -L/home/li/work/tools/lindo/lindoapi/bin/linux64 -m64 -llindo64 -lmosek64 -lconsub3 -lc -ldl -lm -lguide -lpthread -lsvml -limf -lirc
but there comes another problem when run the executable file ex_addinst: after run:
./ex_addinst
there comes:
./ex_addinst: error while loading shared libraries: liblindo64.so.6.0: cannot open shared object file: No such file or directory
The tricky thing is, liblindo64.so.6.0 is in the lib folder which contains:
libconsub3.so libirc.so liblindojni.so libmosek64.so.5.0 lindo.par
libguide.so liblindo64.so liblindojni.so.6.0.3 libsvml.so placeholder
libimf.so liblindo64.so.6.0 libmosek64.so lindoapivars.sh runlindo
I have created symbolic links between liblindo64.so.6.0 and liblindo64.so with
ln -sf liblindo64.so.6.0 liblindo64.so
but it doesn't help.
Can anyone tell me what is wrong here?
(I am not sure I should put this question in a new post, but I think currently it is better to follow the old one)
Ok, lindo.h contains the prototypes for those functions, but where are the functions actually defined? If they're in another C file you need to compile that one too, and link both the object files together.
If the functions are part of another static library, you need to tell the linker to link that library along with your object file.
If they're defined with a shared library, you can probably get g++ to still link to it at compile time, and take care of the library loading etc. Otherwise you'll need to load the library at runtime and reference the functions from the library. This Wikipedia article on dynamic loading of shared libraries contains some example code.
Try
g++ -Wall -o ex_addinst ex_addinst.c
instead of
g++ -Wall -o ex_addinst ex_addinst.o
You want to compile the .c file, not the .o file.
You need to tell gcc to link with the library or object file(s) that contain the LS... functions you're using. The header file tells the compiler how to call them, but the linker needs to know where to get the compiled code from.
undefined reference to ... is not a declaration problem. The compiler fails because it can't find symbols (objects) which are related to those declared functions.
In your case, you use the Limbo API, and include the header file, but you don't tell the compiler to link with the library : that's why it doesn't find symbols.
EDIT : I had forgotten the part when you say you're new to Linux. To link with the library, you need to use the -L/-l options of g++. man g++ is always a good read, and the Limbo's documentation should be, too.