g++ can't find libavcodec, but gcc can [duplicate] - c++

This question already has an answer here:
linking with libavcodec, still seeing undefined references
(1 answer)
Closed 4 months ago.
I'm on Ubuntu 20.04 trying to compile some code that uses libav. Take the following example script:
// main.c
#include <libavcodec/avcodec.h>
int main()
{
avcodec_find_encoder((enum AVCodecID) 0);
return 0;
}
If I build this with gcc test.c -lavcodec it builds just fine, but if I build it with g++ test.c -lavcodec I get:
/usr/bin/ld: /tmp/ccHxMTp1.o: in function `main':
test.c:(.text+0xe): undefined reference to `avcodec_find_encoder(AVCodecID)'
collect2: error: ld returned 1 exit status

I think you're 'suffering' from C++ name mangling. Try wrapping the #include line with an extern "C" {, } pair...
extern "C" {
#include <libavcodec/avcodec.h>
}
int main()
{
avcodec_find_encoder((enum AVCodecID) 0);
return 0;
}

Related

undefined reference to dlopen

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

Error " undefined reference to `std::ios_base" while linking cpp header only library to fortran [duplicate]

This question already has an answer here:
Linking Fortran and C++ binaries using gcc
(1 answer)
Closed 3 years ago.
I am trying to link a header only library (which is in cpp) to a fortran code. I am using this example to test my library.
$ cat cppfunction.C
#include<cmath>
#include<mylib/mylib.hpp>
extern "C"
{
void cppfunction_(float *a, float *b);
}
void cppfunction_(float *a, float *b)
{
*a=7.0;
*b=9.0;
}
$ cat fprogram.f
program fprogram
real a,b
a=1.0
b=2.0
print*,"Before fortran function is called"
print*,'a=',a
print*,'b=',b
call cppfunction(a,b)
print*,"After cpp function is called"
print*,'a=',a
print*,'b=',b
stop
end
For compiling I am using:
$ gfortran -c fprogram.f
$ g++ -c cppfunction.C
$ gfortran -lc -o fprogram fprogram.o cppfunction.o
This runs fine if I remove my library header. But have this error when included:
cppfunction.o: In function `__static_initialization_and_destruction_0(int, int)':
cppfunction.C:(.text+0xa1): undefined reference to `std::ios_base::Init::Init()'
cppfunction.C:(.text+0xb0): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
Anything I might be doing wrong?
You're not linking the C++ standard library:
gfortran -lc -lstdc++ -o fprogram fprogram.o cppfunction.o
// ^^^^^^^^

Defining macros for a source file form the command line

I am trying to define a macro for the source file from the command line on an ubuntu system using the -D flag .
The source file is:
#include<iostream>
using namespace std;
int factorial(int n){
if(n!=1){
return(n * factorial(n-1));
}
else return 1;
#ifdef DEEPAK
cout<<"hello"<<endl;
#endif
}
int main()
{
factorial(4);
return(0);
}
The command I am typing is:
gcc -Wall -DDEEPAK factorial.cpp -o main
BUT, I am getting the error:
/tmp/cc4Ii5l2.o: In function `__static_initialization_and_destruction_0(int, int)':
factorial.cpp:(.text+0x63): undefined reference to `std::ios_base::Init::Init()'
factorial.cpp:(.text+0x72): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
Any help would be deeply appreciated. Thanks.
You should use g++ instead of gcc in the command, because gcc doesn't link to the C++ STL by default, and hence it gives an undefined reference to std::ios_base.
g++ -Wall -DDEEPAK factorial.cpp -o main

collect2: error: ld returned 1 exit status [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I used the following:
gcc -c -O4 ab_test.c
This worked and generated ab_test.o without error, but
gcc -o ab_test ab_test.o -lgsl -lgslcblas -lm
lead to error as:
**/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../lib64/crt1.o: In function `_start':
/home/abuild/rpmbuild/BUILD/glibc-2.18/csu/../sysdeps/x86_64/start.S:118: undefined reference to `main'
collect2: error: ld returned 1 exit status**
The code is ab_test.c is as under
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
Main()
{
int i, temp_int;
char amode[30];
char bmode[30];
float wave_vector_y;
for(i=0; i<41; i++)
{
//// set wave vector ////
wave_vector_y = i*0.005;
temp_int = 10000*wave_vector_y;
sprintf(amode,"a%04d.dat",temp_int);
sprintf(bmode,"b%04d.dat",temp_int);
}
}
Your "main" signature should be something like int main(void)or int main(int argc ,char *argv[]) and not old C style syntax for int Main().

Inluding functions in header file [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
I have header file:
dictionary.h:
#ifndef dictionary_h__
#define dictionary_h__
extern char *BoyerMoore_positive(char *string, int strLength);
extern char *BoyerMoore_negative(char *string, int strLength);
extern char *BoyerMoore_skip(char *string, int strLength);
#endif
function definations: dictionary.cpp
#include<stdio.h>
#include<string.h>
char *BoyerMoore_positive(char *string, int strLength)
{
} ---- //for each function
and main file main.cpp:
#include "dictionary.h"
#pragma GCC diagnostic ignored "-Wwrite-strings"
using namespace std;
void *SocketHandler(void *);
int main(int argv, char **argc)
{
----
skp = BoyerMoore_skip(ch[i], strlen(ch[i]) );
if(skp != NULL)
{
i++;
printf("in\n");
continue;
}
printf("\n hi2 \n");
str = BoyerMoore_positive(ch[i], strlen(ch[i]) );
str2= BoyerMoore_negative(ch[i], strlen(ch[i]) );
----
}
When I execute main.cpp
it gives:
/tmp/ccNxb1ix.o: In function `SocketHandler(void*)':
LinServer.cpp:(.text+0x524): undefined reference to `BoyerMoore_skip(char*, int)'
LinServer.cpp:(.text+0x587): undefined reference to `BoyerMoore_positive(char*, int)'
LinServer.cpp:(.text+0x5bd): undefined reference to `BoyerMoore_negative(char*, int)'
collect2: error: ld returned 1 exit status
I dont know why it could not find the function!
Help appreciated!
You need to compile both source files into main.o and dictionary.o and then link these object file together into the final executable:
$ g++ -c main.cpp
$ g++ -c dictionary.cpp
$ g++ -o myexe main.o dictionary.o
Or you can build and link in one go:
$ g++ -o myexe main.cpp dictionary.cpp
You'd normally create a Makefile to take the drudgery out of this process, which might be as little as (untested):
myexe: main.o dictionary.o
Then it's simply:
$ make
Are you sure that your dictionary.cpp is included to your project and built without errors?
Linker can't find those functions in object-files after compilation, check out full log for compilation error or success of your dictionary.cpp file.