everyone. I am using aarch64-linux-android-g++ to compile a binary.
The code is this:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int diag_wakelock_destroy();
int main(int argc, char **argv){
printf("hello world");
char mystring[] = "hi";
int result = diag_wakelock_destroy();
printf("finish! return: %d\n", result);
return 0;
}
The function diag_wakelock_destroy is defined in libdiag.so as we can see from IDA or just use "nm -D libdiag.so". But when I compiled it with this command
aarch64-linux-android-g++ --sysroot=$SYSROOT -g -fpie -pie -o exe_fine hi_diag.cpp libdiag.so liblog.so libcutils.so libc++.so libm.so libc.so ld-android.so -ldl -L.
I got this error.
/tmp/ccUb4ImK.o: In function `main':
/home/sam/Documents/test/lib-diag/hi_diag.cpp:13: undefined reference to
`diag_wakelock_destroy()'
collect2: error: ld returned 1 exit status
So it can't find that exported function! This is weird. Can anyone help me ? Thanks!
Related
I have one C program which I want to load into my running C program. Following are the snippet
File : a.c
#include <stdio.h>
void abc() {
printf("This is abc\n");
}
File : mainFile.cpp
#include<stdio.h>
#include <dlfcn.h>
int main(int argc, char **argv) {
void *lib = dlopen("./a.so", RTLD_LAZY);
if (!lib) {
printf("dlopen failed: %s\n", dlerror());
return 1;
}
void (*f)() = dlsym(lib, "abc");
if (f) {
f();
} else {
printf("dlsym for f1 failed: %s\n", dlerror());
}
dlclose(lib);
return 0;
}
I am compiling with the following commands
gcc -fpic -g -shared -ldl -o a.so a.c
g++ -w mainFile.cpp -o mainFile
Output:
/tmp/cc9fYZaf.o: In function `main':
mainFile.cpp:(.text+0x1a): undefined reference to `dlopen'
collect2: error: ld returned 1 exit status
I am compiling in Ubuntu 16.04 with gcc version gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
Please help
Note: I have followed the following references but none helped.
Can you dynamically compile and link/load C code into a C program?
undefined reference to `dlopen' since ubuntu upgrade
undefined reference to `dlopen'
The second line — the one which links the executable — needs the -ldl, not the first:
g++ -w mainFile.cpp -ldl -o mainFile
I want to use pacman to install OpenGL but I get an error compiling.
I want to link with -lglut -lGLU -lGL but what packages are those? Is there a way to find out?
#include <iostream>
using namespace std;
#include <SDL2\SDL.h>
#include <GL\glu.h>
int main(int arc, char *argv[]) {
cout << "Hello world" << endl;
glShadeModel(GL_SMOOTH);
return 0;
}
I run (obviously I get an error saying the lib is not found if using -lGLU)
g++ *.cpp
This is the error
C:\Users\User\AppData\Local\Temp\ccztMdnY.o:main.cpp:(.text+0x39): undefined reference to `__imp_glShadeModel'
F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
Why is uuid_generate_random undefined? As far as I know I am including the uuid library when compiling.
Any advice whats going wrong?
me#me-vm:~/Projects/_Tests/test_cba$ make
g++ -o res main.cpp -Luuid -std=c++11
/tmp/ccRubbJa.o: In function `main':
main.cpp:(.text+0x30): undefined reference to 'uuid_generate_random'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
Simple code:
#include <iostream>
#include <uuid/uuid.h>
int main(int argc, char** argv) {
uuid_t id;
uuid_generate_random(id);
return 0;
}
Using wrong command.Use below
g++ -o res main.cpp -luuid -std=c++11
-L is used to provide location of library
-l is used to provide name library.
I'm trying to build simple programm using ffmpeg
#include <stdio.h>
#include <stdlib.h>
#define __STDC_CONSTANT_MACROS
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavdevice/avdevice.h>
#include <libavutil/time.h>
}
#include <iostream>
int main( int argc, char* argv[] )
{
AVCodec *icodec;
AVFormatContext *ifcx = NULL;
AVInputFormat *ifmt;
AVCodecContext *iccx;
AVStream *ist;
AVStream *ost;
AVPacket pkt;
int i_index;
int64_t timenow, timestart;
int got_key_frame = 0;
AVFormatContext *ofcx;
const char *sProg = argv[ 0 ];
const char *sFileInput;
const char *sFileOutput;
int64_t bRunTime;
bRunTime = atoi( argv[ 2 ] ) * 1000000;
// Initialize library
av_log_set_level( AV_LOG_DEBUG );
av_register_all();
avcodec_register_all();
avformat_network_init();
avdevice_register_all();
And i get these errors
g++ -o rtsp3 -I/usr/include -I/usr/local/include rtsp3.cpp -lavformat -lavcodec -lavutil -lm -lz -lva -lpthread
/tmp/ccAXDgvi.o: In function main':
rtsp3.cpp:(.text+0x115): undefined reference toavdevice_register_all'
/usr/local/lib/libavformat.a(matroskadec.o): In function matroska_decode_buffer':
/home/user/projects/ffmpeg-git/ffmpeg/libavformat/matroskadec.c:1242: undefined reference toBZ2_bzDecompressInit'
/home/user/projects/ffmpeg-git/ffmpeg/libavformat/matroskadec.c:1257: undefined reference to BZ2_bzDecompress'
/home/user/projects/ffmpeg-git/ffmpeg/libavformat/matroskadec.c:1250: undefined reference toBZ2_bzDecompressEnd'
/home/user/projects/ffmpeg-git/ffmpeg/libavformat/matroskadec.c:1262: undefined reference to `BZ2_bzDecompressEnd'
collect2: error: ld returned 1 exit status
I have git version of ffmpeg and successfully compiled it and make install.
Try this, it worked for me:
gcc -o main.o main.c `pkg-config --cflags --libs libavformat libavutil`
You need to link with libavdevice, e.g. -lavdevice
Also, apparently, libbz2, e.g. -lbz2
I solved this by installing libbz2-dev
On Ubuntu: sudo apt-get install libbz2-dev
check if you have object files for libav format added in the path. You have added librarians properly in the makefile so problem shouldn't have come.
If you are using ubuntu, check if this is preset
/usr/lib/x86_64-linux-gnu/libavformat.so
If not, you need to install libavformat-dev
you can install it using "sudo apt-get install libavformat-dev"
I was facing the same issues and it got resolved after this.
I am getting an undefined reference to main error - even though I have defined main, and (AFAICT), I have linked it correctly. Here is my code and the commands I used:
// ################################################
//proj1.h
#ifndef __SCRATCH_PROJ1_H
#define __SCRATCH_PROJ1_H
int addOne(int i);
#endif /*__SCRATCH_PROJ1_H */
// ################################################
//proj1.cpp
#include "proj1.h"
int addOne(int i){
return i+1;
}
// ################################################
//proj1_unittest.cpp
#include "proj1.h"
#include "gtest/gtest.h"
// Test Function
TEST(addOneTest, Positive) {
EXPECT_EQ(1,addOne(0));
EXPECT_EQ(2,addOne(1));
EXPECT_EQ(40320, addOne(40319));
}
TEST(addOneTest, Negative) {
EXPECT_FALSE(addOneTest(-1));
}
GTEST_API_ int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Here is the console output:
$ g++ -isystem -pthread -c ${SOURCE_DIR}/proj1.c -o ${SOURCE_DIR}/proj1.o
$ g++ -o ${SOURCE_DIR}/mytest ${SOURCE_DIR}/*.o -L${GTEST_DIR} libgtest.a
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
Why is the main() function not been found by the linker?
As far as I can see you are not compiling file proj1_unittest.cpp (as your code comment has it) / proj1_unittest.c (as your console output implies).