"Undefined symbols for architecture x86_64:" when compiling lex and yacc - gcc

Lex and Yacc work, but when I try to compile the y.tab.c file, I'm getting the following error. I have a feeling its a linking issue where compiler can't find the library that it needs, but I have no idea how to fix it.
In my nor.y file, I have this at the top:
%{
#include <stdio.h>
%}
In my nor.l file, I have this at the top:
%{
#include "y.tab.h"
#include <stdlib.h>
extern YYSTYPE yylval;
%}
Then, when I try to compile with
gcc y.tab.h -ly -ll
I get the error:
Undefined symbols for architecture x86_64:
"_yyparse", referenced from:
_main in liby.a(main.o)
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

You need to compile the .c files, not the header file:
gcc y.tab.c lex.yy.c -ly -ll

Related

FFMPEG library usage for own C++ project

I would like to create my own project using FFMPEG to build a player, but creating a new file to include ffmpeg library seems not easy. I have configure the ffmpeg and make build.
./configure
./make
The ffmpeg hello world program (myffmpeg.c):
#include <libavformat/avformat.h>
int main(int argc, char *argv[]) {
av_register_all();
return 0;
}
but it shows
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
Undefined symbols for architecture x86_64:
"av_register_all()", referenced from:
_main in myffmpeg-61ec1b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation
When I try to link the allformats.o file, which has av_register_all function inside. I got:
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
Undefined symbols for architecture x86_64:
"_av_register_input_format", referenced from:
_register_all in allformats.o
"_av_register_output_format", referenced from:
_register_all in allformats.o
"_avcodec_register_all", referenced from:
_register_all in allformats.o
"_ff_a64_muxer", referenced from:
_register_all in allformats.o
...
"_ff_yuv4mpegpipe_muxer", referenced from:
_register_all in allformats.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 should be my bad Makefile knowledge, could anyone give me some hint on how to call ffmpeg library functions? Or even how to modify the Makefile to build my own program? Thanks!
*Update:
It could be my compiling command problem, is it the way to compile?
g++ myffmpeg.c
maybe the problem is that c++ is decorating functions. So the same function compiled with C compiler and C++ looks different to linker. try this:
extern "C"
{
#include <libavformat/avformat.h>
}
this code means that you told your compiler to not decorate functions in that file.
I think I could answer myself.
g++ -o main.o main.c `pkg-config --cflags --libs libavformat`
I don't know the reason now, it is from https://soledadpenades.com/2009/11/24/linking-with-ffmpegs-libav/
We still need the extern, see answer below.

JNI build C file (Undefined symbols for architecture x86_64)

I'm trying to build a .c file on OSX but I keep getting:
Undefined symbols for architecture x86_64
#include "Test.h"
JNIEXPORT jint JNICALL Java_Test_test
(JNIEnv * env, jclass cls, jbyteArray s, jlong ss, jbyteArray sss, jlong ssss, jbyteArray sssss, jlong ssssss) {
/*printf("Hello World!\n");*/
return 1;
}
Built with:
clang++ -o test -I/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/include/darwin Test.cpp
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)
Is there something I need to install?
clang++ -o test Test.cpp is trying to compile and link to produce a runnable executable. You didn't define main(), so of course this fails when it tries to link the CRT code (which calls the user-supplied main().)
Perhaps you meant to use clang++ -fPIC -shared -o libtest.so Test.cpp -I... (keeping all the other options the same), to make a shared library like JNI needs. Or .dylib or .jnilib, whatever libraries are normally called on your platform.
If you're following a tutorial that didn't tell you how to compile your code, it's probably not a very good tutorial and you should find better documentation.

error on boost filesystem

I try to use boost filesystem on my Mac. I only added the following header and got an error when I tried to compile
# include <boost/filesystem.hpp>
the error is
Undefined symbols for architecture x86_64:
"boost::system::generic_category()", referenced from:
__static_initialization_and_destruction_0(int, int) in cclyDZox.o
"boost::system::system_category()", referenced from:
__static_initialization_and_destruction_0(int, int) in cclyDZox.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
any help is appreciated
You need to add the boost_system library:
-lboost_system
to your build command. Note that you will also need to provide the -lboost_system library too.
If your project thereafter builds but moans with something like
dyld: Library not loaded: libboost_filesystem.dylib
you just need to set the environment variable
DYLD_LIBRARY_PATH
to include your
$BOOST_HOME/lib
directory as mentioned here.
I think you should do
g++ -I~/Documents/boost_1_53_0/include -L~/Documents/boost_1_53_0/stage/lib -std=c++11 test1ver1.cpp -lboost_filesystem -lboost_system
The
-I flag points to where the Boost headers are
-L flag points to where the Boost libs are
-lboost_filesystem and -lboost_system flags enabled the link of your binary and the Boost Filesystem and System shared libraries. (Those should be always after the object or source files)
For instance:
#include <boost/filesystem.hpp>
int main() {
boost::filesystem::path path_household_csv("./test");
}

Link external library GCC OS X

I have installed GCC 4.7.2 and GMP 5.1.0 and I've written this simple code in main.cpp locate at ~/Desktop:
#include <iostream>
#include <gmp.h>
using namespace std;
int main ()
{
mpz_t a;
mpz_init(a);
mpz_set_ui(a, 42);
cout << "Hello, world!" << endl;
}
I compile it with:
$ g++ main.cpp -o exe
but I get this error message:
Undefined symbols for architecture x86_64:
"___gmpz_init", referenced from:
_main in ccC0FXun.o
"___gmpz_set_ui", referenced from:
_main in ccC0FXun.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I think it's because it doesn't find the GMP library, am I right?
So how can I link an external library such as GMP in GCC?
Your program works fine for me here using:
g++ main.cpp -o exe -lgmp
Check the GCC documentation for a description of the -l flag.

OpenGL + GLEW + MinGW application linking issue

I'm getting some undefined references when building my project. Here's the build log:
**** Build of configuration Debug for project test ****
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\main.o ..\src\main.cpp
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\test.o ..\src\test.cpp
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\window.o ..\src\window.cpp
..\src\window.cpp: In member function 'void Window::StartRenderContext()':
..\src\window.cpp:150:24: warning: NULL used in arithmetic
..\src\window.cpp:161:28: warning: NULL used in arithmetic
..\src\window.cpp:174:24: warning: NULL used in arithmetic
g++ -mwindows -l glew32 -l glew32s -l glu32 -l opengl32 -o test.exe src\window.o src\test.o src\main.o
src\window.o: In function `ZN6Window18StartRenderContextEv':
C:\eclipse\workspace\test\Debug/../src/window.cpp:101: undefined reference to `wglCreateContext#4'
C:\eclipse\workspace\test\Debug/../src/window.cpp:102: undefined reference to `wglMakeCurrent#8'
C:\eclipse\workspace\test\Debug/../src/window.cpp:115: undefined reference to `glewInit'
C:\eclipse\workspace\test\Debug/../src/window.cpp:125: undefined reference to `wglMakeCurrent#8'
C:\eclipse\workspace\test\Debug/../src/window.cpp:126: undefined reference to `wglDeleteContext#4'
C:\eclipse\workspace\test\Debug/../src/window.cpp:148: undefined reference to `__wglewChoosePixelFormatARB'
C:\eclipse\workspace\test\Debug/../src/window.cpp:159: undefined reference to `__wglewChoosePixelFormatARB'
C:\eclipse\workspace\test\Debug/../src/window.cpp:185: undefined reference to `__wglewCreateContextAttribsARB'
C:\eclipse\workspace\test\Debug/../src/window.cpp:194: undefined reference to `__wglewCreateContextAttribsARB'
C:\eclipse\workspace\test\Debug/../src/window.cpp:204: undefined reference to `__wglewCreateContextAttribsARB'
C:\eclipse\workspace\test\Debug/../src/window.cpp:214: undefined reference to `__wglewCreateContextAttribsARB'
C:\eclipse\workspace\test\Debug/../src/window.cpp:227: undefined reference to `wglMakeCurrent#8'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 8128 ms.
Here's my link command:
g++ -mwindows -l glew32 -l glew32s -l glu32 -l opengl32 -o test.exe src\window.o src\test.o src\main.o
Is this correct? I'm using the 64-bit binaries of glew (I think the 32s don't mean anything). Were they only meant to be used with visual studio?
Here's the includes in my code:
#include "Windows.h"
#include "GL/glew.h"
#include "GL/wglew.h"
#include "GL/gl.h"
#include "GL/glu.h"
#include "test.h"
I am using Eclipse Indigo CDT, MinGW, Win32, OpenGL, and glew.
I solved "glew undefined reference" problems.
My development environment is eclipse CDT with MinGW on Windows 7 (x64).
The solution is the following 3 steps:
Add source code: #define GLEW_STATIC
Add linker flag: -lglew32s -lopengl32 -lfreeglut
Add compiling flag: gcc -DGLEW_STATIC
If needed, you have to add -lglu32 -glut32 etc.

Resources