g++ Mac Curl Undefined symbols for architecture x86_64 - c

I'm following an online course related to curl. However, I met some problems.
My building statement is:
gcc -Wall web-server.c -o web-server1 -lcurl
This is the code in web-server.c
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
int main(void){
CURL *curl = NULL;
CURLcode res;
curl = curl_easy_init();
curl_easy_setops(curl, CURLOPT_URL, "http://www.google.com");
res = curl_easy_perform(curl);
if (res != CURLE_OK){
print("curl easy perform error res = %d",res);
return 1;
}
curl_easy_cleanup(curl);
return 0;
}
And the error message is:
Undefined symbols for architecture x86_64:
"_curl_easy_setops", referenced from:
_main in web-server-afa9ad.o
"_print", referenced from:
_main in web-server-afa9ad.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Thank you guys so much
My operating system is MacOs 10.14.6
My gcc's version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Found CUDA installation: /usr/local/cuda, version unknown
Detailed error message
"/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library
/Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -no_deduplicate -
dynamic -arch x86_64 -macosx_version_min 10.14.0 -o web-server
/var/folders/j2/jrqt57vj73s0jc8mjc2mddf40000gn/T/web-server-14b13c.o -lcurl
-lSystem
/Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/lib/darwin/libclan
g_rt.osx.a

Related

_lua_close undefined symbols on my mac

I have source file
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>
#define BUFSIZE 1024
int main() {
char line[BUFSIZE];
lua_State *L = lua_open();
while (fgets(line, sizeof(line), stdin) != 0) {
lua_dostring(L, line);
}
lua_close(L);
exit(0);
}
Then I run gcc luac.c, got an error.
Undefined symbols for architecture x86_64:
"_lua_close", referenced from:
_main in luac-9bbdf6.o
"_lua_dostring", referenced from:
_main in luac-9bbdf6.o
"_lua_open", referenced from:
_main in luac-9bbdf6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
It seems like that I have no lua.h in my /usr/local/include folder. But it have.
ls -al /usr/local/include/lua.h
-rw-r--r--# 1 root admin 11K Apr 3 16:23 /usr/local/include/lua.h
And I have
ls -al /usr/local/lib/liblua.a
-rw-r--r-- 1 root admin 150448 Apr 3 16:23 /usr/local/lib/liblua.a
The gcc search path
gcc -Xlinker -v
(#)PROGRAM:ld PROJECT:ld64-305
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
Library search paths:
/usr/lib
/usr/local/lib
Framework search paths:
/Library/Frameworks/
/System/Library/Frameworks/
...
Why gcc says error, can you tell me.
Updated:
The problem is gcc default doesn't add lua lib?
I use gcc luac.c -llua -llualib, works.

Link tidylib library to C application

I am trying to use the tidylib library within my C application. When compiling, I get the following errors:
$ make
rm -f sbo-export
cc sbo-export.c safarilib.c -L/usr/local/lib -lcurl -L/usr/local/Cellar/libtidy/lib -I/usr/local/Cellar/libtidy/include -o sbo-export
Undefined symbols for architecture x86_64:
"_tidyCreate", referenced from:
_safari_init_session in safarilib-c7ab6a.o
"_tidyParseString", referenced from:
_safari_init_session in safarilib-c7ab6a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [default] Error 1
safarilib.c:
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <tidy/tidy.h>
#include <tidy/buffio.h>
#include <curl/curl.h>
#include "safarilib.h"
void usage( char *username, char *password )
{
TidyDoc tdoc = tidyCreate();
char *input = "<html><body><h1>Hello World!</h1></body></html>";
tidyParseString( tdoc, input );
}
Any suggestions?
Further Infos:
For installing libtidy, I did the following:
Downloaded libtidy from http://tidy.sourceforge.net and then
followed the instructions form the file tidy/build/readme.txt
My library is installed here:
/usr/local/Cellar/libtidy
/usr/local/Cellar/libtidy/bin
/usr/local/Cellar/libtidy/bin/tab2space
/usr/local/Cellar/libtidy/bin/tidy
/usr/local/Cellar/libtidy/include
/usr/local/Cellar/libtidy/include/buffio.h
/usr/local/Cellar/libtidy/include/platform.h
/usr/local/Cellar/libtidy/include/tidy.h
/usr/local/Cellar/libtidy/include/tidyenum.h
/usr/local/Cellar/libtidy/lib
/usr/local/Cellar/libtidy/lib/libtidy-0.99.0.dylib
/usr/local/Cellar/libtidy/lib/libtidy.a
/usr/local/Cellar/libtidy/lib/libtidy.dylib
/usr/local/Cellar/libtidy/lib/libtidy.la
From compiler option it's look you have not specified tidylib.
cc sbo-export.c safarilib.c -L/usr/local/lib -lcurl -L/usr/local/Cellar/libtidy/lib -I/usr/local/Cellar/libtidy/include -o sbo-export
Here you need to add -ltidy and probably path by -L .
Some additional info about linking.

Compiling Unix code on Mac

We were given course work to create the game Mine Sweeper. We are still early in the semester so this homework shouldn't be too difficult.
We were given header and source files that are to be used for the visual part of the program.
Main problem is that I can't compile these files on my Mac. Here is what I get:
$ gcc mineSweeper.c -I.
Undefined symbols for architecture x86_64:
"_colorPrint", referenced from:
_main in mineSweeper-4b9486.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Also tried this:
$ gcc mineSweeper.c -I. -arch i386
Undefined symbols for architecture i386:
"_colorPrint", referenced from:
_main in mineSweeper-0938b1.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
gcc version:
gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
OSX version:
Software OS X 10.9.5 (13F34)
And finally the code we were provided with:
//colorPrint.h
//defines possible colors for the foreground color.
typedef enum
{
FG_Def = 0,
FG_Black = 30,
FG_Red,
FG_Green,
FG_Yellow,
FG_Blue,
FG_Magenta,
FG_Cyan,
FG_White
}fgColor;
//defines possible colors for the background color.
//BG_Def prints with the default background color of the terminal.
typedef enum
{
BG_Def = 0,
BG_Black = 40,
BG_Red,
BG_Green,
BG_Yellow,
BG_Blue,
BG_Magenta,
BG_Cyan,
BG_White
}bgColor;
//defines possible additional attributes for the color printing.
//normally, you would use ATT_Def.
typedef enum
{
ATT_Def = 0,
ATT_Bright = 1,
ATT_Underline = 4,
ATT_Reverse = 7,
ATT_Hidden = 8,
ATT_Scratch = 9
}attribute;
//clears the screen completely.
void clearScreen();
//prints a format string with its arguments (like printf!),
//in the specified foreground color, background color, and attribute.
void colorPrint(fgColor fg, bgColor bg, attribute att, char* format,...);
//colorPrint.c
#include <stdio.h>
#include <colorPrint.h>
#include <stdarg.h>
void clearScreen()
{
printf("\e[1;1H\e[2J");
}
void colorPrint(fgColor fg, bgColor bg, attribute att, char* format,...)
{
va_list args;
if(bg != BG_Def)
printf("\e[%d;%d;%dm",att,fg,bg);
else
printf("\e[%d;%dm",att,fg);
va_start (args, format);
vprintf(format, args);
va_end (args);
printf("\e[0m");
}
There is another header and code for receiving the char from user but I'm assuming linking it is irrelevant.
Any sort of help is welcome.. thanks in advance :)
PS. I also have a PC if it helps to switch to windows.
PPS. I'm keeping VirtualBox as a last resort.
You're trying to compile and link mineSweeper.c into a final executable on its own, but that file is not a complete program, it depends on a function defined in another file.
You either need to compile and link all the files in one step:
gcc mineSweep.c colourPrint.c
or compile each file separately and then link the objects:
gcc -c mineSweeper.c
gcc -c colorPrint.c
gcc mineSweeper.o colorPrint.o
I'm surprised your course didn't explain how to compile programs consisting of more than one file.
A simple makefile will ease the process:
mineSweeper: mineSweeper.o colorPrint.o
$(CC) $^ $(LDLIBS) -o $#

Is libsasl2 broken on OSX Yosemite? Missing sasl_client_done

I've been having some trouble with the installed version of libsasl2 (Cyrus SASL).
In particular, it seems that the local headers and sasl_version report version 2.1.26, but no symbol is provided for the global function sasl_client_done.
I'm pretty sure I'm supposed to have that symbol because:
It's there in the provided sasl/sasl.h header
The cyrsus sasl NEWS file lists "Implemented sasl_client_done()/sasl_server_done()" as a 2.1.24 feature
It's there everywhere that provides 2.1.26 outside of Yosemite
For a reproduction:
note that the sample below prints
"impl: 'Cyrus SASL', version: 33619994, major: 2, minor: 1, step: 26"
the sample compiles and executes on a linux install with the same library version after uncommenting the code
the uncommented code produces a link error on yosemite
Undefined symbols for architecture x86_64:
"_sasl_client_done", referenced from:
_main in foo-072675.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
invoking the compiler with:
clang -Wall -Werror -lsasl2 -o foo foo.c -v
with clang -v of:
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.10.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name foo.c -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 241.9 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -Wall -Werror -fdebug-compilation-dir /Users/jcarey/work -ferror-limit 19 -fmessage-length 0 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.10.0 -fencode-extended-block-signature -fdiagnostics-show-option -vectorize-slp -o /var/folders/wq/jypwqgv976n0db5l5qxw900r0000gq/T/foo-92054e.o -x c foo.c
clang -cc1 version 6.0 based upon LLVM 3.5svn default target x86_64-apple-darwin14.0.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -o foo -lsasl2 /var/folders/wq/jypwqgv976n0db5l5qxw900r0000gq/T/foo-92054e.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"_sasl_client_done", referenced from:
_main in foo-92054e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
And the code in question:
#include <sasl/sasl.h>
#include <stdio.h>
#include <stdint.h>
int main(int argc, char ** argv) {
const char *impl;
int version;
uint32_t buf;
uint16_t major;
uint8_t minor;
uint8_t step;
sasl_version(&impl, &version);
buf = version;
major = buf >> 24;
minor = (buf << 8) >> 24;
step = (buf << 24) >> 24;
printf("impl: '%s', version: %d, major: %d, minor: %d, step: %d\n", impl, version, major, minor, step);
/*
{
int (* scd)(void);
scd = &sasl_client_done;
printf("sasl_client_done: %p\n", scd);
}
*/
return 0;
}
I'm thinking that something's screwy with the way cyrus sasl got packaged for Yosemite (using a symbol list from Mavericks perhaps?).
As a matter of interest, I just checked with 10.10.4 and I see the symbol is now there:
$ nm /usr/lib/libsasl2.dylib |grep sasl_client_done
000000000000724a T _sasl_client_done
The sample code now works fine (with the commented section now uncommented). The same Cyrus SASL version is still returned though:
impl: 'Cyrus SASL', version: 33619994, major: 2, minor: 1, step: 26
sasl_client_done: 0x7fff8e3dc24a

fatal error: 'X11/Xlib.h' file not found

I have installed XQuartz.
I compiled using g++:
g++ -o -lX11 -I/opt/X11/include window2.cc
Error
Undefined symbols for architecture x86_64:
"_XCreateWindow", referenced from:
_main in window2-dXb9bZ.o
"_XFlush", referenced from:
_main in window2-dXb9bZ.o
"_XMapWindow", referenced from:
_main in window2-dXb9bZ.o
"_XOpenDisplay", referenced from:
_main in window2-dXb9bZ.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If i compile like this:
g++ window2.cc -o window -lX11 -I/opt/X11/include
Error
ld: library not found for -lX11
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Im sure Xlib.h is in /opt/X11/include
Code:
#include <X11/Xlib.h>
#include <unistd.h>
int main()
{
// Open a display.
Display *d = XOpenDisplay(0);
if ( d )
{
// Create the window
Window w = XCreateWindow(d, DefaultRootWindow(d), 0, 0, 200,
100, 0, CopyFromParent, CopyFromParent,
CopyFromParent, 0, 0);
// Show the window
XMapWindow(d, w);
XFlush(d);
// Sleep long enough to see the window.
sleep(10);
}
return 0;
}
How do I solve this problem ? Thanks in advance
Problem resolved. In case anyone who's interested:
You have to compile like this:
g++ -o window window.cc -I/usr/X11R6/include -L/usr/X11R6/lib -lX11
Try
cc -I /opt/X11/include/ test.c -L /opt/X11/lib -lX11

Resources