MacOS ld: symbols not found (x86_64) - c++

I have the following error:
Undefined symbols for architecture x86_64:
"_inflateEnd", referenced from:
uWS::Hub::~Hub() in main.o
"_inflateInit2_", referenced from:
uWS::Hub::Hub(int, bool, unsigned int) 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)
I have checked all the similar questions and their solutions but nothing worked for me.
I am trying to test uWebSockets (https://github.com/uNetworking/uWebSockets) and have the following file structure:
my_app (which has main.cpp)
ext/uWebSockets (which is a clone of the repository above)
So, I am doing the following:
~/ext/uWebSockets$ make
make `(uname -s)`
c++ -std=c++11 -O3 -I src -shared -fPIC src/Extensions.cpp src/Group.cpp src/Networking.cpp src/Hub.cpp src/Node.cpp src/WebSocket.cpp src/HTTPSocket.cpp src/Socket.cpp src/Epoll.cpp -stdlib=libc++ -mmacosx-version-min=10.7 -undefined dynamic_lookup -L/usr/local/opt/openssl/lib -I/usr/local/opt/openssl/include -o libuWS.dylib
and obtain libuWS.dylib in /ext/uWebSockets
Then, I do the following:
~/my_app$ g++ -c main.cpp -o main.o -I../ext/uWebSockets/src -I/usr/local/opt/openssl/include -std=c++11
So now I have main.o in /my_app. But when I'm trying to:
~/my_app$ g++ -o start main.o -L../ext/uWebSockets -luWS
I receive the aforementioned error:
Undefined symbols for architecture x86_64:
"_inflateEnd", referenced from:
uWS::Hub::~Hub() in main.o
"_inflateInit2_", referenced from:
uWS::Hub::Hub(int, bool, unsigned int) 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)
Here is the same command with -v:
~/my_app$ g++ -o start main.o -L../ext/uWebSockets -luWS -v
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
"/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -dynamic -arch x86_64 -macosx_version_min 10.13.0 -o start -L../ext/uWebSockets main.o -luWS -lc++ -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"_inflateEnd", referenced from:
uWS::Hub::~Hub() in main.o
"_inflateInit2_", referenced from:
uWS::Hub::Hub(int, bool, unsigned int) 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)
How can I link them correctly?
main.cpp:
#include <iostream>
#include "uWS.h"
int main() {
uWS::Hub h;
h.onError([](void *user) {
std::cout << "WebSocket: Error has occured" << std::endl;
});
h.onConnection([](uWS::WebSocket<uWS::CLIENT> *ws, uWS::HttpRequest req) {
std::cout << "Client established a remote connection over non-SSL" << std::endl;
});
h.onDisconnection([](uWS::WebSocket<uWS::CLIENT> *ws, int code, char *message, size_t length) {
std::cout << "Client got disconnected with data: " << ws->getUserData() << ", code: " << code << ", message: <" << std::string(message, length) << ">" << std::endl;
});
// url, user, headers, timeout, group client
h.connect("wss://www.test.com/", (void *) 0, {}, 5000);
h.run();
std::cout << "Falling through testConnections" << std::endl;
return 0;
}

Finally, I was able to fix it. The problem was in zlib, which I didn't include.
So the correct way of running this is g++ -o start main.o -luWS -lz

Related

gtkmm-4.0 will not link on macOS Monterey

I'm having issues getting gtkmm-4.0 to link on macOS Monterey.
I'm trying to compile this example program.
#include <gtkmm.h>
class MyWindow : public Gtk::Window {
public:
MyWindow();
};
MyWindow::MyWindow() {
set_title("Basic application");
set_default_size(200, 200);
}
int main(int argc, char* argv[]) {
auto app = Gtk::Application::create("org.gtkmm.examples.base");
return app->make_window_and_run<MyWindow>(argc, argv);
}
After trying to compile with g++-11 base.cc -o base `pkg-config gtkmm-4.0 --cflags --libs`
I get the following error:
Undefined symbols for architecture x86_64:
"__ZN3Gtk15ShortcutManager20add_controller_vfuncERKSt10shared_ptrINS_18ShortcutControllerEE", referenced from:
__ZTV8MyWindow in ccyzyk08.o
__ZTC8MyWindow0_N3Gtk6WindowE in ccyzyk08.o
__ZTC8MyWindow48_N3Gtk15ShortcutManagerE in ccyzyk08.o
"__ZN3Gtk15ShortcutManager23remove_controller_vfuncERKSt10shared_ptrINS_18ShortcutControllerEE", referenced from:
__ZTV8MyWindow in ccyzyk08.o
__ZTC8MyWindow0_N3Gtk6WindowE in ccyzyk08.o
__ZTC8MyWindow48_N3Gtk15ShortcutManagerE in ccyzyk08.o
"__ZN3Gtk6Widget14snapshot_vfuncERKSt10shared_ptrINS_8SnapshotEE", referenced from:
__ZTV8MyWindow in ccyzyk08.o
__ZTC8MyWindow0_N3Gtk6WindowE in ccyzyk08.o
__ZTC8MyWindow0_N3Gtk6WidgetE in ccyzyk08.o
"__ZN3Gtk6Widget16on_query_tooltipEiibRKSt10shared_ptrINS_7TooltipEE", referenced from:
__ZTV8MyWindow in ccyzyk08.o
__ZTC8MyWindow0_N3Gtk6WindowE in ccyzyk08.o
__ZTC8MyWindow0_N3Gtk6WidgetE in ccyzyk08.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
Does anyone know why these errors occur and how to fix them?
I'm using macOS Monterey on an Intel Mac.
I found a similar unanswered question from someone running macOS Monterey on arm64.
For reference, here is the output of `pkg-config gtkmm-4.0 --cflags --libs`:
-mfpmath=sse -msse -msse2 -I/usr/local/Cellar/libffi/3.4.2/include -I/usr/local/Cellar/gtkmm4/4.6.0/include/gtkmm-4.0 -I/usr/local/Cellar/gtkmm4/4.6.0/lib/gtkmm-4.0/include -I/usr/local/Cellar/pangomm/2.50.0/include/pangomm-2.48 -I/usr/local/Cellar/pangomm/2.50.0/lib/pangomm-2.48/include -I/usr/local/Cellar/glibmm/2.70.0/include/giomm-2.68 -I/usr/local/Cellar/glibmm/2.70.0/lib/giomm-2.68/include -I/usr/local/Cellar/glibmm/2.70.0/include/glibmm-2.68 -I/usr/local/Cellar/glibmm/2.70.0/lib/glibmm-2.68/include -I/usr/local/Cellar/cairomm/1.16.1/include/cairomm-1.16 -I/usr/local/Cellar/cairomm/1.16.1/lib/cairomm-1.16/include -I/usr/local/Cellar/libsigc++/3.2.0/include/sigc++-3.0 -I/usr/local/Cellar/libsigc++/3.2.0/lib/sigc++-3.0/include -I/usr/local/Cellar/gtk4/4.6.1/include/gtk-4.0/unix-print -I/usr/local/Cellar/gtk4/4.6.1/include/gtk-4.0 -I/usr/local/Cellar/glib/2.70.4/include/gio-unix-2.0 -I/usr/local/Cellar/libepoxy/1.5.9_1/include -I/usr/local/Cellar/cairo/1.16.0_5/include/cairo -I/usr/local/Cellar/pango/1.50.5/include/pango-1.0 -I/usr/local/Cellar/harfbuzz/4.0.1/include/harfbuzz -I/usr/local/Cellar/pango/1.50.5/include/pango-1.0 -I/usr/local/Cellar/fribidi/1.0.11/include/fribidi -I/usr/local/Cellar/harfbuzz/4.0.1/include/harfbuzz -I/usr/local/Cellar/graphite2/1.3.14/include -I/usr/local/Cellar/gdk-pixbuf/2.42.6/include/gdk-pixbuf-2.0 -I/usr/local/Cellar/libpng/1.6.37/include/libpng16 -I/usr/local/Cellar/libtiff/4.3.0/include -I/usr/local/Cellar/cairo/1.16.0_5/include/cairo -I/usr/local/Cellar/pixman/0.40.0/include/pixman-1 -I/usr/local/Cellar/fontconfig/2.13.1/include -I/usr/local/opt/freetype/include/freetype2 -I/usr/local/Cellar/libpng/1.6.37/include/libpng16 -I/usr/local/Cellar/libxcb/1.14_2/include -I/usr/local/Cellar/libxrender/0.9.10/include -I/usr/local/Cellar/libxext/1.3.4/include -I/usr/local/Cellar/libx11/1.7.3.1/include -I/usr/local/Cellar/libxcb/1.14_2/include -I/usr/local/Cellar/libxau/1.0.9/include -I/usr/local/Cellar/libxdmcp/1.1.3/include -I/usr/local/Cellar/graphene/1.10.6/include/graphene-1.0 -I/usr/local/Cellar/graphene/1.10.6/lib/graphene-1.0/include -I/usr/local/Cellar/glib/2.70.4/include -I/usr/local/Cellar/glib/2.70.4/include/glib-2.0 -I/usr/local/Cellar/glib/2.70.4/lib/glib-2.0/include -I/usr/local/opt/gettext/include -I/usr/local/Cellar/pcre/8.45/include -I/usr/local/Cellar/xorgproto/2021.5/include -L/usr/local/Cellar/gtkmm4/4.6.0/lib -L/usr/local/Cellar/pangomm/2.50.0/lib -L/usr/local/Cellar/glibmm/2.70.0/lib -L/usr/local/Cellar/cairomm/1.16.1/lib -L/usr/local/Cellar/libsigc++/3.2.0/lib -L/usr/local/Cellar/gtk4/4.6.1/lib -L/usr/local/Cellar/pango/1.50.5/lib -L/usr/local/Cellar/harfbuzz/4.0.1/lib -L/usr/local/Cellar/gdk-pixbuf/2.42.6/lib -L/usr/local/Cellar/cairo/1.16.0_5/lib -L/usr/local/Cellar/graphene/1.10.6/lib -L/usr/local/Cellar/glib/2.70.4/lib -L/usr/local/opt/gettext/lib -lgtkmm-4.0 -lpangomm-2.48 -lgiomm-2.68 -lglibmm-2.68 -lcairomm-1.16 -lsigc-3.0 -lgtk-4 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -lgdk_pixbuf-2.0 -lcairo-gobject -lcairo -lgraphene-1.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl

main function not being detected when compiled

I am trying to run a program that opens a window. the purpose is to get the program started opening a window is the start of all programs right?
But when I run my code for some reason I get this error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
However, in my code I do have the main() function so why am I getting this error?
This is my code:
#include <SDL2/SDL.h>
#include <SDL2_image/SDL_image.h>
#include <SDL2_ttf/SDL_ttf.h>
#include <stdio.h>
int main(){
if(SDL_Init( SDL_INIT_EVERYTHING ) < 0){
std::cout << "error 1\n";
std::cout << SDL_GetError();
std::cout << "\n";
return -1;
}
if(TTF_Init() < 0){
std::cout << "error 2\n";
std::cout << TTF_GetError();
std::cout << "\n";
return -1;
}
SDL_Window* window = SDL_CreateWindow("test", 0, 0, 500, 500, 0);
if(!window){
std::cout << "error 3\n";
std::cout << SDL_GetError();
std::cout << "\n";
return -1;
}
int windowid = SDL_GetWindowID(window);
SDL_Renderer* Renderer = SDL_CreateRenderer(window, -1, 0);
running = true;
SDL_Event event;
while(running){
while(SDL_PollEvent(&event)){
if(event.type == SDL_WindowEvent){
if(event.window.windowID == windowid){
if(event.window.type == SDL_WindowClose){
Destroywindow(window);
running = false;
}
}
}
}
}
return 0;
}
my make file looks like this:
#!/bin/bash
brew update
brew install sdl2
g++ -o /Users/mikahshattuck/noneproject/none2019-05-0909-22-
14:2:/none.app/Contents/MacOS/mainrun.cpp -I /Library/Frameworks -l
SDL2
exit 0
this is the full out:
Already up-to-date.
Warning: sdl2 2.0.9_1 is already installed and up-to-date
To reinstall 2.0.9_1, run `brew reinstall sdl2`
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
thank you in advance
When using SDL on macOS or Windows, you need to add -Dmain=SDL_main to your compile flags and -lSDL2main to your link flags. Since you're using Homebrew, you can make it easier and just use pkg-config to get the correct flags. Use this compiler command as a template and adapt it to your needs:
g++ $(pkg-config --cflags sdl2) -I /Library/Frameworks source.cpp -o output_executable $(pkg-config --libs sdl2)
However, it seems you are also using SDL_ttf, not just plain SDL. In this case, you should probably use SDL2_ttf instead of sdl2 as the package argument of pkg-config:
g++ $(pkg-config --cflags SDL2_ttf) -I /Library/Frameworks source.cpp -o output_executable $(pkg-config --libs SDL2_ttf)
The SDL2_ttf package depends on the sdl2 package, so using SDL2_ttf will also emit the needed flags for sdl2.
The names of the pkg-config packages correspond to *.pc files installed by Homebrew into /usr/local/lib/pkgconfig.

leveled::DB::Open() undefined after I installed leveldb and snappy, macOS

I want to verify the leveldb installation. I have a main function only containing levelDB::DB::Open() function. I installed both snappy and leveldb by using brew install. I have boost 1.67 installed too. I have GCC 8.1. I am running macOS 10.13.5.
my source file looks like this:
int main(void) {
leveldb::DB *db;
leveldb::Options options;
options.create_if_missing = true;
auto err = leveldb::DB::Open(options, "../tmpDB", &db);
if (err.ok()) {
std::cout << "success" << std::endl;
}else {
std::cout << "failed" << std::endl;
}
delete db;
return 0;
}
I compile my code I use g++ main.cpp -lleveldb -lsnappy -o test. The compiler generate the error like:
Undefined symbols for architecture x86_64:
"leveldb::DB::Open(leveldb::Options const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, leveldb::DB**)", referenced from:
_main in ccYcksfh.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
How can I solve this link error?
You can install leveldb with compiler version.
brew reinstall leveldb --cc=gcc-4.8
Can't link against leveldb on OSX

Xcode gives Mach-O linker error: "_JNI_CreateJavaVM", referenced from: _main in main.o

I get this error from testing JNI:
Undefined symbols for architecture x86_64:
"_JNI_CreateJavaVM", 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)
Here is c++ code:
#include <jni.h>
#include <iostream>
using namespace std;
int main()
{
int res;
JavaVMInitArgs vm_args;
JavaVMOption options[3];
JavaVM *jvm;
JNIEnv *env;
jmethodID mid;
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = "-Djava.class.path = /Users/stephen/course/test/Test";
options[2].optionString = "-verbose:NONE";
vm_args.version = JNI_VERSION_1_8;
vm_args.nOptions = 3;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
res = JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);
if(res == JNI_ERR){
cout << "Error invoking the JVM";
return 1;
}
cout <<"create JVM successfully!"<<endl;
jclass cls = env->FindClass("/Users/stephen/course/Qt-project/test/Test");
if(cls != 0){
cout<<"find class successfully!" << endl;
}
mid = env->GetMethodID(cls,"sayHello","stephen");
if(mid != 0){
cout<<"Invoke method successfully!" << endl;
}
jvm->DestroyJavaVM();
return 0;
}
Here is java code:
public class Test
{
public static void sayHello(String s){
System.out.print("hello I am" + s + "\n");
}
}
I add the include path of " jdk/include; jdk/include/darwin" the project, also I add lib path of " jdk/jre/lib/server" to the project to get the libjvm.dylib. The c++ standard library of my project is libstdc++(gnu c++ standard library.
But I can't solve this problem as expected.
Take a look here for a sample code where JVM library is linked with your project:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo028
Take a look at Makefile. Especially, here:
main: recipeNo028_main.o
ld -o lib/recipeNo028_main -L${JAVA_HOME}/jre/lib/server/ \
-ljvm \
$(MAC_OS_FLAGS) \
lib/recipeNo028_main.o
where jvm lib is linked with the code, and here:
CC=llvm-gcc
MAC_OS_FLAGS=-rpath ${JAVA_HOME}/jre/lib/server -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -demangle -dynamic -arch x86_64 -macosx_version_min 10.12.0 -lSystem
where all required libs are added to your code as well. It should work. Try to compile sample code. You can find more samples here: http://jnicookbook.owsiak.org
Update
How to use arbitrary JDK version for compilation.
First, take a look at all installations you have
/usr/libexec/java_home -V
This will produce something like this
/usr/libexec/java_home -V
Matching Java Virtual Machines (4):
9, x86_64: "Java SE 9" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
1.8.0_144, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
1.8.0_111, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
Then, before running make, simply set JAVA_HOME to whatever you like
export JAVA_HOME=$(/usr/libexec/java_home -v 9)
Now, your code will use version that you have chosen.

Error linking personal library which uses log4cplus

I have a weird problem trying to link a simple code which use a personal library which uses lib4cplus.
The file with the main code is so easy and doesn't use log4cplus. I only use lib4cplus into the library. The library compiles correctly but when i link the main file with the library it thowrs errors like that (loggers var not defined under architecture x86_64)
g++ leer.cpp -c
ar r libfichero.a blowfish.o clase.o
g++ -o Class leer.o -lm -lz -llog4cplus libfichero.a
Undefined symbols for architecture x86_64:
"fichero::loggerFichero", referenced from:
fichero::startLoggers() in libfichero.a(clase.o)
fichero::fichero()in libfichero.a(clase.o)
fichero::fichero()in libfichero.a(clase.o)
"fichero::errorLoggerFichero", referenced from:
fichero::startLoggers() in libfichero.a(clase.o)
fichero::fichero()in libfichero.a(clase.o)
fichero::fichero()in libfichero.a(clase.o)
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make[1]: *** [all] Error 1
make: *** [p21] Error 2
The errors comes when i want to define my private logger vars (ficheroLogger and errorFicheroLogger).
That's the code in the cpp file of my library.
bool fichero::startLoggers()
{
// Aquí inico los loggers que voy a usar en la clase.
loggerFichero = Logger::getInstance(LOG4CPLUS_TEXT("utils"));
errorLoggerFichero = Logger::getInstance("BigFail");
}
// Método para definir los logs.
bool fichero::initLogFichero()
{
cout << "Entering LOG Config part..." << endl;
LogLog::getLogLog()->setInternalDebugging(true);
Logger root = Logger::getRoot();
// Load the properties
PropertyConfigurator::doConfigure("ficheroLog4cplus.properties");
Logger fileLog = Logger::getInstance(LOG4CPLUS_TEXT("filelogger"));
// Log with INFO level
LOG4CPLUS_INFO(fileLog, "Application startup");
cout << "Exiting the main part()..." << endl;
// Log with INFO level
if (fileLog.isEnabledFor(INFO_LOG_LEVEL))
{
LOG4CPLUS_WARN(fileLog, "Application shutdown");
}
startLoggers();
return true;
}
// Constructor y Destructor
fichero::fichero()
{
nomFile = "file.txt";
delimitador = "";
hayDelimiatador = false;
initLogFichero();
LOG4CPLUS_WARN(loggerFichero, "Inicio normal");
LOG4CPLUS_WARN(errorLoggerFichero, "Inicio error.");
}
This does not look a like a problem of log4cplus. Try to reshuffle the command line:
g++ -o Class leer.o libfichero.a -llog4cplus -lz -lm