Interface from C-Programm in XCode to SWI-Prolog - c

i want to interface from c-code using XCode IDE under MAC OS X to SWI-Prolog.
I´ve included the header files and use the following sample code:
#include <stdio.h>
#include <SWI-Prolog.h>
#include <SWI-Stream.h>
int main(int argc, const char * argv[])
{
char *av[10];
int ac = 0;
av[ac++] = "/opt/local/lib/swipl-6.2.2/bin/i386-darwin12.2.1/swipl";
av[ac++] = "-x";
av[ac++] = "mystate";
av[ac] = NULL;
#ifdef READLINE /* Remove if you don't want readline */
PL_initialise_hook(install_readline);
#endif
if ( !PL_initialise(ac, av) )
PL_halt(1);
PL_halt(PL_toplevel() ? 0 : 1);
printf("done...\n");
return 0;
}
I also linked at the build settings to the header and lib paths:
Header Search Path: /opt/local/lib/swipl-6.2.2/include
Library Search Path: /opt/local/lib/swipl-6.2.2/lib/i386-darwin12.2.1
But while running the code i get the following errors:
Undefined symbols for architecture x86_64:
"_PL_halt", referenced from:
_main in main.o
"_PL_initialise", referenced from:
_main in main.o
"_PL_toplevel", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Does anyone can help me to get my code running?
Thanks in Advance and Kind regards
Solick

Even if you set the correct paths in the settings, it doesn't automatically find the correct library to link with. You have to add the actual library as well.

I work in Ubuntu (and yes, in Windows, still), and I've come to appreciate the pkg-config support.
If you are already comfortable with it, note that when compiled locally, SWI-Prolog generates a directory ~/lib/pkgconfig.
Then you could add that directory to pkg-config configuration, and use the SW with (IMHO) a good support.

Related

"ERROR:Undefined symbols for architecture x86_64" has occurred in LibSBMLSim(SBML=Systems Biology Markup Language)

I tried to use LibSBMLSim as api in c language.
I referenced https://fun.bio.keio.ac.jp/software/libsbmlsim/ and installed both LibSBML and LibSBMLSim.Then I made c file like below:
/* Example C, C++ code */
#include "libsbmlsim/libsbmlsim.h"
int main(void) {
/*
* Simulate sbml.xml to time=20 with dt=0.1, print_interval=10
* by 4th-order Runge-Kutta Method.
*/
myResult *r = simulateSBMLFromFile("sbml.xml", 20, 0.1, 10, 0, MTHD_RUNGE_KUTTA, 0);
write_csv(r, "result.csv"); /* Export simulation result as CSV file */
free_myResult(r); /* Free Result object */
return 0;
}
And executed "gcc test.c -o test", but error has occurred. Error messages are below:
Undefined symbols for architecture x86_64:
"_free_myResult", referenced from:
_main in test-f56b85.o
"_simulateSBMLFromFile", referenced from:
_main in test-f56b85.o
"_write_csv", referenced from:
_main in test-f56b85.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I checked /usr/local/include/libsbmlsim/libsbmlsim.h, there specified free_myResult function.
I tried a lot but it doesn't work. Please help.
I checked /usr/local/include/libsbmlsim/libsbmlsim.h, there specified free_myResult function.
That only confirms that the prototypes are present. But when you compile, you need to tell what library to use to find those symbols. So you need to link with the library using -lsbmlsim and probably specify the location of where to search for the library using -L and the location of header files using -I too -- all these in your command line.
Alternatively you can use a Makefile. Have a look at the Makefile provided in the libsbmlsim's example.

embedding Julia in C/C++ on OSX

I am trying to compile a very simple C/C++ program to call Julia functions. Following the instructions that you find on the Julia documentation page, I set up my link path to /Users/william.calhoun/Desktop/romeo/lib/julia looking for libjulia.so and I set up my include path to /Users/william.calhoun/Desktop/romeo/include/julia looking for julia.h
I have a C file called test.c which runs the following code:
#include <stdio.h>
#include "skeleton.h"
#include <julia.h>
int main(int argc, const char * argv[]) {
jl_init(NULL);
/* run julia commands */
jl_eval_string("print(sqrt(2.0))");
/* strongly recommended: notify julia that the
program is about to terminate. this allows
julia time to cleanup pending write requests
and run all finalizers
*/
jl_atexit_hook();
return 0;
}
However this yields the following error:
Undefined symbols for architecture x86_64:
"_jl_atexit_hook", referenced from:
_main in test.o
"_jl_eval_string", referenced from:
_main in test.o
"_jl_init", referenced from:
_main in test.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am not doing anything other than calling functions defined properly (hopefully) within the Julia source code. What am I doing wrong? This seems like the simplest example and I can't figure it out.
Any help would be much appreciated!
Linking to libjulia (libjulia.dynlib on OS/X)
This error is a result of not linking to libjulia, as all of the symbols (_jl_atexit_hook, _jl_eval_string, _jl_init) are located in that library. Broadly, for all 3 of the following platforms (Windows, OS/X, Linux), the approach is similar, and though the location of the libjulia library is different on Windows than the other 2 this stackoverflow question is applicable. Also to be completely accurate, on OS/X, dynamic libraries have the extension .dynlib not .so as they do on Linux.
The link step
For simplicity, assuming you've compiled to object code (there is a file called embed.o), here's the link step.
cc -o embed embed.o -L/Users/william.calhoun/Desktop/romeo/lib/julia -Wl,-rpath,/Users/william.calhoun/Desktop/romeo/lib/julia -ljulia
There are 2 important things to note here.
Linking using -ljulia will allow the linker to resolve all of the above symbols.
Since this is a dynamic library and that dynamic library is located in a non standard location (e.g. not in /usr/lib), the dynamic linker will not be able to find it at run time unless you give it special instructions on how to find it. The -rpath directive causes the linker to insert the path /Users/william.calhoun/Desktop/romeo/lib/juliainto the list of paths to search.

Undefined symbols for architecture x86_64 (clang)

I'm trying to use OpenSSL to compute sha1 hash from a c program. I am compiling with clang on Mac OS X Yosemite with an Intel i7 (so 64 bit).
The relevant piece of code is roughly like so:
#include <openssl/evp.h>
...
unsigned char outHash[20];
hash("SHA1","abcd", 20, outHash);
The thing is, when using the "hash" function from openssl/evp.h, compiling with clang yields the following error:
Undefined symbols for architecture x86_64:
"_hash", referenced from:
_getRandomSHA1 in main-68ccd6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So it looks like OpenSSL is not found by the linker (the "hash" function is unidentified). Any ideas on how to fix this?
EDIT:
It turns out that I was trying to use a function that does not exist ("hash") - sorry for misleading you about that.
However I'm still having quite the same problem: including openssl/evp.h does not seem to work.
This is the hash function that I am using, it uses evp to perform sha1 encoding:
unsigned int hash(const char *mode, const char* dataToHash, size_t dataSize, unsigned char* outHashed) {
unsigned int md_len = -1;
const EVP_MD *md = EVP_get_digestbyname(mode);
if(NULL != md) {
EVP_MD_CTX mdctx;
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
EVP_DigestUpdate(&mdctx, dataToHash, dataSize);
EVP_DigestFinal_ex(&mdctx, outHashed, &md_len);
EVP_MD_CTX_cleanup(&mdctx);
}
return md_len;
}
And then I call:
hash("SHA1","abcd", 20, outHash);
I am compiling same as before, this is my compile command (pretty simple):
clang main.c
And I'm getting the following error from the linker:
Undefined symbols for architecture x86_64:
"_EVP_DigestFinal_ex", referenced from:
_hash in main-935849.o
"_EVP_DigestInit_ex", referenced from:
_hash in main-935849.o
"_EVP_DigestUpdate", referenced from:
_hash in main-935849.o
"_EVP_MD_CTX_cleanup", referenced from:
_hash in main-935849.o
"_EVP_MD_CTX_init", referenced from:
_hash in main-935849.o
"_EVP_get_digestbyname", referenced from:
_hash in main-935849.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
#include <openssl/evp.h>
...
unsigned char outHash[20];
hash("SHA1","abcd", 20, outHash);
OpenSSL does not have a int hash(...) or char* hash(...) function.
$ cat /usr/include/openssl/evp.h | grep hash returns 0 hits. man 3 hash returns BSD's "hash database access method".
Undefined symbols for architecture x86_64:
"_hash", referenced from:
_getRandomSHA1 in main-68ccd6.o
Latching on to _getRandomSHA1, there is a SHA function, but it's available from sha.h, and not evp.h (I filtered out the DEPRECATED_IN... macro):
$ cat /usr/include/openssl/sha.h | grep SHA | grep char
...
unsigned char *SHA(const unsigned char *d, size_t n, unsigned char *md);
...
I believe it uses a static buffer, and its use is discouraged.
The project recommends using EVP Message Digests interface now.
(EDIT): I am compiling same as before, this is my compile command
(pretty simple):
clang main.c
See Linking OpenSSL libraries to a program. You need to add -lcrypto to the end of the compile command.
(EDIT, from comment): I have openssl at /opt/local/include/openssl....
You are going to have more problems because you are probably runtime linking against the wrong version of the OpenSSL library. For that problem, see Statically link OpenSSL in XCode. The same problem exists from the command line and Xcode.
Here's the easiest solution. Notice that it does not use -lcrypto (-lfoo is usually the solution, except when runtime linking to the wrong shared object becomes a problem):
clang main.c -I /opt/local/include /opt/local/lib/libcrypto.a -o my_hash.exe
An archive is just a collection of object files, so an archive can be used in place where an object file is required.
OS X does not honor RPATHs. Another solution is to build OpenSSL and your program with an RPATH so the correct version of the library is loaded at runtime. For that, see Compilation and Installation | Using RPATHs on the OpenSSL wiki and Build OpenSSL with RPATH? on Stack Overflow.
Which SDK are you using, and which version of Xcode? OpenSSL was deprecated in OS X 10.7 and removed from the 10.11 SDK, which ships with the Xcode 7 Beta. Downloading OpenSSL separately should work, though you may need to change your project configuration for the new location.

Undefined symbols when using ODE in C

I'm trying to use ODE in my C project on Xcode 4.5.1 (MacOS 10.8). I used
./autogen.sh
./configure
make
sudo make install
to install the lib. I then tried to compile a test program:
#include <stdio.h>
#include <ode/ode.h>
int main(int argc, const char * argv[]) {
dWorldID world = dWorldCreate();
return 0;
}
I added dSINGLE in Build Settings->Preprocessor Macros and libode.a in my Xcode project.
When I try to build and run I get a bunch of errors like
Undefined symbols for architecture x86_64:
"std::terminate()", referenced from:
_dJointGroupCreate in libode.a(ode.o)
_dBodyCreate in libode.a(ode.o)
dxJointBall::dxJointBall(dxWorld*) in libode.a(ball.o)
dxJointBall::dxJointBall(dxWorld*) in libode.a(ball.o)
dxJointFixed::dxJointFixed(dxWorld*) in libode.a(fixed.o)
dxJointFixed::dxJointFixed(dxWorld*) in libode.a(fixed.o)
dxJointHinge::dxJointHinge(dxWorld*) in libode.a(hinge.o)
...
.
.
.
.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The very same procedure works fine if I create a C++ project instead of a C one.
Did I forget to add something in the build settings in order to make it work?
Solved adding
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/lib/libstdc++.dylib
to the Xcode project.

C Problem with Compiler?

I just started learning C, and wrote my hello world program:
#include <stdio.h>
main()
{
printf("Hello World");
return 0;
}
When I run the code, I get a really long error:
Apple Mach-O Linker (id) Error
Ld /Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Products/Debug/CProj normal x86_64
cd /Users/Solomon/Desktop/C/CProj
setenv MACOSX_DEPLOYMENT_TARGET 10.7
/Developer/usr/bin/clang -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.7.sdk -L/Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Products/Debug -F/Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Products/Debug -filelist /Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Intermediates/CProj.build/Debug/CProj.build/Objects-normal/x86_64/CProj.LinkFileList -mmacosx-version-min=10.7 -o /Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Products/Debug/CProj
ld: duplicate symbol _main in /Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Intermediates/CProj.build/Debug/CProj.build/Objects-normal/x86_64/helloworld.o and /Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Intermediates/CProj.build/Debug/CProj.build/Objects-normal/x86_64/main.o for architecture x86_64
Command /Developer/usr/bin/clang failed with exit code 1
I am running xCode
Should I reinstall DevTools?
have you tried
int main() or even int main(int argc, char**argv) ?
The error message indicates that the symbol _main (which refers to the main() function) is defined twice, once in helloworld.o and once in main.o.
It's likely that you have two source files, helloworld.c and main.c, in the same project, and that both define a function named main.
You can only have one main function in a program. Removing one of those two source files (and the associated object file) from your Xcode project should fix the problem. (I haven't used Xcode myself; perhaps someone else can tell you how to do that, if it's not obvious.)
(And the correct definition is int main(void), not the old-style main(), but I don't think that's related to the symptom you're seeing.)
There are a couple of stackoverflow questions that could be closely related to yours:
Xcode duplicate symbol _main
Xcode, Duplicate symbol _main

Resources