Undefined symbols for architecture x86_64 (clang) - c

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.

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.

Allow double floating point comparisons with Unity unit testing framework from Throw The Switch

I'm having some trouble somewhere with the linker while trying to compile a test written using the Unity testing framework by Throw The Switch. I have other tests that compile and run perfectly fine so I'm definitely just missing something in enabling the assertion helper for double floating point comparisons.
There is documentation in the header file telling us how to enable double floating point comparisons.
* - define UNITY_INCLUDE_DOUBLE to allow double floating point comparisons
However, I end up with this error:
Undefined symbols for architecture x86_64:
"_UnityAssertDoublesWithin", referenced from:
_test_example in main-6fae82.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 wrote up the simplest example possible to duplicate this:
#define UNITY_INCLUDE_DOUBLE
#include "unity.h"
void test_example(void)
{
TEST_ASSERT_EQUAL_DOUBLE(1.234, 1.234);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_example);
return UNITY_END();
}
The contents of the CWD of the example and the exact command used to call clang (showing the same error again):
$ ls
main.c unity.c unity.h unity_internals.h
$ clang unity.c main.c
Undefined symbols for architecture x86_64:
"_UnityAssertDoublesWithin", referenced from:
_test_example in main-ee77c2.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Same exact thing happens with gcc (but it's actually just calling clang under the hood).
I'm pretty sure I'm just missing one tiny step but for the life of me I can't see what it is right now. Thanks in advance for your help.
I have this working on my minimal tests by using the same define in the unity_config.h file and adding UNITY_INCLUDE_CONFIG_H as a compiler -D flag (gcc). But running with the #define in the source does not do the same trick
You can find a full copy of a unity_config.h file on the ThrowTheSwitch GitHub repo. I usually just throw it in the same folder Unity is in.
It also works if you add the define directly into the unity.h file as specified in the comments in that file (that you quote in your question) which says
All options described below should be passed as a compiler flag to all files using Unity. If you must add #defines, place them BEFORE the #include above.
i.e. in unity.h
#define UNITY_INCLUDE_DOUBLE
#include "unity_internals.h"
...
Why your initial tactic, which should essentially be the same thing, does not work, I do not know.
Looks like this works when defining UNITY_INCLUDE_DOUBLE with the -D flag to clang/gcc. For example:
$ clang -DUNITY_INCLUDE_DOUBLE -DUNITY_DOUBLE_PRECISION=1e-12f unity.c main.c
$ ./a.out
main.c:10:test_example:PASS
-----------------------
1 Tests 0 Failures 0 Ignored
OK

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.

Interface from C-Programm in XCode to SWI-Prolog

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.

Undefined symbols for architecture x86_64: (Mac OS X 10.7)

I am working on an MP for my CS class. Our computer labs are working under Linux OS, but I tried compiling the code on my home computer (Mac OS X). I am getting the following error:
Undefined symbols for architecture x86_64:
"_tdestroy", referenced from:
_dictionary_destroy in libdictionary.o
_dictionary_destroy_free in libdictionary.o
ld: symbol(s) not found for architecture x86_64
I tried finding a solution online, but I was unsuccessful. We are using the following macros in the Makefile:
CC = gcc
INC = -I.
FLAGS = -g -W -Wall
LIBS = -lpthread
Any ideas?
From the GNU man page of tdestroy:
SVr4, POSIX.1-2001. The function tdestroy() is a GNU extension
This means that this function is not available on OS X
EDIT:
Put this after the includes:
#ifndef _GNU_SOURCE
void tdestroy(void *root, void (*free_node)(void *nodep)) { }
#endif
You can try to implement tdestroy by using twalk/tdelete/free - it should'n be very hard to do, but leaving it empty should work too (but it will create a memory leak on OSX).
EDIT 2: added link to the man page (10x to Cameron)

Resources