Compiler failed with error Undefined symbols for architecture x86_64
macOS 10.15.6 Catalina
compiler
g++-11 --version
g++-11 (Homebrew GCC 11.2.0_3) 11.2.0
error :
Undefined symbols for architecture x86_64:
"__ZN4ssdb6Client7connectEPKci", referenced from:
_main in cciJLzzz.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
.cpp
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include "SSDB_client.h"
int main(int argc, char **argv){
const char *ip = (argc >= 2)? argv[1] : "127.0.0.1";
int port = (argc >= 3)? atoi(argv[2]) : 8888;
ssdb::Client *client = ssdb::Client::connect(ip, port);
if(client == NULL){
printf("fail to connect to server!\n");
}else{
printf("ssdb client\n");
}
delete client;
return 0;
}
is there a notation issue here ?
namespace ssdb, class client ..
if I replace this line :
ssdb::Client *client = ssdb::Client::connect(ip, port);
with :
ssdb::Client *client = 0;
the compiler works and I don't get this error ..
the above code is from ssdb docs :
ssdb docs
ssdb docs C++ client
I would like to connect to the ssdb server ..
EDIT :
For command requests
g++ -o hello-ssdb hello-ssdb.cpp libssdb-client.a
..in libssdb-client.a(SSDB_impl.o)
..in libssdb-client.a(link.o)
ld: symbol(s) not found for architecture x86_64
g++ -o hello-ssdb.cpp hello-ssdb libssdb-client.a
ld: can't link with a main executable file
Regarding libssdb-client.a
ar t libssdb-client.a
__.SYMDEF
SSDB_impl.o
bytes.o
link.o
link_addr.o
..
using ssdb for long time but with php client. fine in most cases, but there is something I have to iterate many many times .. and with C/C++ I should be able to save a lot of time ..
..
Workaround :
package main
import (
"fmt"
"os"
"ssdb"
)
func main() {
fmt.Println("We will use Golang .. \n")
ip := "127.0.0.1"
port := 8888
db, err := ssdb.Connect(ip, port)
if err != nil {
os.Exit(1)
}
defer db.Close()
var val interface{}
keys := []string{}
keys = append(keys, "c");
keys = append(keys, "d");
val, err = db.Do("multi_get", "a", "b", keys);
fmt.Printf("%s\n", val);
db.Set("a", "xxx")
val, err = db.Get("a")
fmt.Printf("%s\n", val)
fmt.Printf("----\n");
return
}
Note that in the documentation for ssdb, they include a libssdb.a that you need to link:
g++ -o hello-ssdb hello-ssdb.cpp libssdb.a
This would have the implementation of all the code for ssdb and is why you are seeing the problem you are seeeing.
Related
This question already has answers here:
gcc compiles libavformat code, but g++ does not [duplicate]
(1 answer)
Undefined reference, using FFMpeg-library (AvCodec) on Ubuntu, 64-bits system
(1 answer)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I write to test ffmpeg and call ffmpeg function, but it alwasy tell me link err: can not found ffmpeg function:
this is my code:
#ifdef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
#endif
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
#include <libavutil/imgutils.h>
#include <libavutil/samplefmt.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
static const char *src_filename = NULL;
static const char *video_dst_filename = NULL;
static const char *audio_dst_filename = NULL;
static AVFormatContext *fmt_ctx = NULL;
int main(int argc, char **argv) {
int ret = 0;
if (argc != 4) {
fprintf(stderr, "usage: %s input_file video_output_file audio_output_file\n"
"API example program to show how to read frames from an input file.\n"
"This program reads frames from a file, decodes them, and writes decoded\n"
"video frames to a rawvideo file named video_output_file, and decoded\n"
"audio frames to a rawaudio file named audio_output_file.\n",
argv[0]);
exit(1);
}
// src_filename = argv[1];
src_filename = "/tmp/a.mp4";
video_dst_filename = argv[2];
audio_dst_filename = argv[3];
/* open input file, and allocate format context */
if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
fprintf(stderr, "Could not open source file %s\n", src_filename);
exit(1);
}
avformat_close_input(&fmt_ctx);
}
the follow is build command:
clang++ -I /usr/local/apps/homebrew/Cellar/ffmpeg/4.4_1/include/ -L/usr/local/apps/homebrew/Cellar/ffmpeg/4.4_1/lib -lavformat main.cpp
and the terminal output:
Undefined symbols for architecture x86_64:
"avformat_open_input(AVFormatContext**, char const*, AVInputFormat*, AVDictionary**)", referenced from:
_main in main-f2a6f0.o
"avformat_close_input(AVFormatContext**)", referenced from:
_main in main-f2a6f0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
then Who can tell me why.
I'm trying to create go app, which uses some C++ code from sdk in shared library.
Added reference to target library with #cgo LDFLAGS:
#cgo LDFLAGS: -L/opt/cprocsp/lib/amd64 -lxades -v
and build go app with
go build -o main .
Compilation goes ok, but linking fails with error
/usr/bin/ld: $WORK/b001/_x003.o: undefined reference to symbol 'CryptReleaseContext'
/usr/bin/ld: //opt/cprocsp/lib/amd64/libcapi10.so.4: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Function CryptReleaseContext exists in /opt/cprocsp/lib/amd64/libxades.so.
With -v option I noticed linker arguments order
/usr/lib/gcc/x86_64-linux-gnu/8/collect2 ... $WORK/b001/_cgo_main.o $WORK/b001/_x001.o $WORK/b001/_x002.o $WORK/b001/_x003.o -lxades -lstdc++ ...
With Google I found that arguments order may be important, but I can't find a way to control them and stucking with it. How can I build app?
Example:
main.go
package main
import "log"
func main() {
data := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<Envelope xmlns=\"urn:envelope\">" +
"<Data>Hello, World!</Data>" +
"<Node xml:id=\"nodeID\">Hello, Node!</Node>" +
"</Envelope>"
signed := Sign(data)
log.Print(signed)
}
signer.go
package main
/*
#cgo LDFLAGS: -L/opt/cprocsp/lib/amd64 -lxades -v
#include <stdarg.h>
#include <stdlib.h>
#include "signer.h"
*/
import "C"
import "log"
import "unsafe"
func Sign(data string) (result string) {
log.Print("Signing message")
c_data := C.CString(data)
defer C.free(unsafe.Pointer(c_data))
c_result := C.CString(result)
defer C.free(unsafe.Pointer(c_result))
C.sign(c_data, c_result)
return C.GoString(c_result)
}
signer.h
void sign(char* src, char *dst);
signer.cpp
SDK's example code, that uses functions from /opt/cprocsp/lib/amd64/libxades.so
...
extern "C" {
void sign(char* src, char *dst){
BYTE *pbToBeSigned = (BYTE *) src;
DWORD cbToBeSigned = strlen(src);
static XADES_SIGN_MESSAGE_PARA signParams = { sizeof(signParams) };
initSignParams(&signParams);
PCRYPT_DATA_BLOB pSignedMessage = 0;
// Создаем подписанное сообщение
if (!XadesSign(&signParams, NULL, FALSE, pbToBeSigned, cbToBeSigned, &pSignedMessage)) {
cout << "XadesSign() failed" << endl;
return;
}
vector<unsigned char> message(pSignedMessage->cbData);
copy(pSignedMessage->pbData, pSignedMessage->pbData + pSignedMessage->cbData, message.begin());
char* result = reinterpret_cast<char*>(message.data());
strcpy(dst, result);
}
}
Update
#cgo LDFLAGS: -L/opt/cprocsp/lib/amd64 -Wl,-rpath,/opt/cprocsp/lib/amd64 -lxades -v
didn't help
answer was in second error:
#cgo LDFLAGS: -L/opt/cprocsp/lib/amd64 -lxades -lcapi10 -lcapi20 -v
first time when I'm trying to add lcapi10 - missed that new error tells about lcapi20, not 10
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.
I've been learning C++ and have decided to try to create a simple file reader using libzip on archive files (e.g. Word).
I’ve recently installed libzip on my Macbook using brew but I seem to keep on getting the following issue whenever I try to compile a program that uses libzip:
Undefined symbols for architecture x86_64:
"_zip_fopen", referenced from:
_main in main-918bfa.o
"_zip_open", referenced from:
_main in main-918bfa.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: *** [a.exe] Error 1
The command I use to compile:
g++ -g main.cpp -std=c++11 -I/usr/local/Cellar/libzip/0.11.2/include -I/usr/local/Cellar/libzip/0.11.2/lib/libzip/include -L/usr/local/Cellar/libzip/0.11.2/lib -o ../a.exe
main.cpp:
#include <iostream>
#include <fstream>
#include <zip.h>
#include <zlib.h>
using namespace std;
int numArgs = 2;
int main(int argc, char** argv){
// Parse command line arguments
if(argc != numArgs){
std::cout << "Incorrect number of arguments provided.\n";
std::cout << "Command line syntax: fileReader.exe inputFile" << endl;
exit(0);
}
// Try out libzip functionality
std::string inputDocument(argv[1]);
int err = 0;
zip* z = zip_open(inputDocument.c_str(), 0, &err);
if(z == NULL) {
printf("Could not read docx file. Error code: %d", err);
exit(-1);
}
zip_file* contentTypes = zip_fopen(z, "[Content_Types].xml", ZIP_FL_UNCHANGED);
exit(0);
}
Doesn't look like your including the libzip library in the compilation command. Try adding -lzip to your g++ command
I'm fairly new to C++ but this thing has me baffled by any logic. My code is as follows:
#include "stdlib.h"
#include "syslog.h"
#include "unistd.h"
#include "sys/stat.h"
#include "X11/Xlib.h"
#include "cstdio"
void process();
void startTracker();
Display *display;
Window rootWindow;
XEvent xevent;
I have the Xlib header included and if I click on member functions in Eclipse it navigates to the definitions.
int main(int argc, char *argv[])
{
// set logging up
openlog("unison", LOG_CONS|LOG_PID|LOG_NDELAY, LOG_LOCAL1);
syslog(LOG_NOTICE, "Starting Unison Handler");
pid_t pid, sid;
pid = fork();
// fork failed
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}
if (chdir("/") < 0) {
exit(EXIT_FAILURE);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
startTracker();
while (true) {
process();
}
closelog();
return(EXIT_SUCCESS);
}
Then I assign the variables for input selection
void startTracker() {
display = XOpenDisplay(0);
rootWindow = XRootWindow(display, 0);
XSelectInput(display, rootWindow, PointerMotionMask);
}
void process()
{
...but when i add the &event here...
XNextEvent(display, &xevent);
switch (xevent.type) {
case MotionNotify:
syslog(
LOG_NOTICE,
"Mouse position is %dx%d",
xevent.xmotion.x_root, xevent.xmotion.y_root
);
}
}
...the whole thing falls apart.
For some reason passing the xevent as reference throws off the entire Xlib header and gives me this:
00:16:15 **** Incremental Build of configuration Debug for project unisond ****
make all
Building file: ../unisond.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"unisond.d" -MT"unisond.d" -o "unisond.o" "../unisond.cpp"
Finished building: ../unisond.cpp
Building target: unisond
Invoking: GCC C++ Linker
g++ -o "unisond" ./unisond.o
./unisond.o: In function `startTracker()':
/home/ancarius/workspace/unisond/Debug/../unisond.cpp:97: undefined reference to `XOpenDisplay'
/home/ancarius/workspace/unisond/Debug/../unisond.cpp:98: undefined reference to `XRootWindow'
/home/ancarius/workspace/unisond/Debug/../unisond.cpp:99: undefined reference to `XSelectInput'
./unisond.o: In function `process()':
/home/ancarius/workspace/unisond/Debug/../unisond.cpp:105: undefined reference to `XNextEvent'
collect2: error: ld returned 1 exit status
make: *** [unisond] Error 1
00:16:15 Build Finished (took 159ms)
At the risk of getting downvoted could someone please explain what I've done wrong? I've tried everything I could think of but no luck.
It looks like you are missing the X11 library for linking.
add -lX11 to the g++ invocation.
This provides the steps required.
Right click on Project Folder> Properties> C/C++ Build > Settings > GCC C++ Linker > Libraries > add "X11"