Undefined reference error in linux

Linux c++ error: undefined reference to ‘dlopen’

I work in Linux with C++ (Eclipse), and want to use a library. Eclipse shows me an error:

Do you know a solution?

Here is my code:

11 Answers 11

You have to link against libdl, add

to your linker options

@Masci is correct, but in case you’re using C (and the gcc compiler) take in account that this doesn’t work:

Took me a bit to figure out.

That’s one annoying «feature» for sure

I was struggling with it when writing heredoc syntax and found some interesting facts. With CC=Clang , this works:

as well as all of these:

  • $CC -ldl -x c -o app.exe —
  • $CC -x c -ldl -o app.exe —
  • $CC -x c -o app.exe -ldl —
  • $CC -x c -o app.exe — -ldl

However, with CC=gcc , only the last variant works; -ldl after — (the stdin argument symbol).

I was using CMake to compile my project and I’ve found the same problem.

The solution described here works like a charm, simply add $ to the target_link_libraries() call

The topic is quite old, yet I struggled with the same issue today while compiling cegui 0.7.1 (openVibe prerequisite).

What worked for me was to set: LDFLAGS=»-Wl,—no-as-needed» in the Makefile.

I’ve also tried -ldl for LDFLAGS but to no avail.

you can try to add this

to the configure options

You needed to do something like this for the makefile:

That’ll pass the linker flags from make through to the linker. Doesn’t matter that the makefile was autogenerated.

I met the same problem even using -ldl .

Besides this option, source files need to be placed before libraries, see undefined reference to `dlopen’.

In order to use dl functions you need to use the -ldl flag for the linker.

how you do it in eclipse ?

Press Project —> Properties —> C/C++ build —> Settings —> GCC C++ Linker —>
Libraries —> in the «Libraries(-l)» box press the «+» sign —> write «dl» (without the quotes)-> press ok —> clean & rebuild your project.

Источник

Читайте также:  Extreme landing pro mac os

Fixing the ‘Undefined Reference’ Errors for C/C++ Projects

This tutorial shows how to various problems related to missing symbols in C/C++ projects. We will create a basic project calling the png_create_read_struct() function from the libpng library and will go through common missing setup steps, explaining the errors that will arise.

Before you begin, install VisualGDB 5.4 or later.

  1. Start Visual Studio and locate the VisualGDB Linux Project Wizard:
  2. Pick a name and location for your project:
  3. Press “Create” to launch the VisualGDB-specific part of the wizard. On the first page, pick Create a new project -> Application -> MSBuild:
  4. In this tutorial we will use a cross-toolchain that runs on Windows and builds code for a Linux target and will demonstrate points of failure caused by this configuration. However, most of the steps shown below will work for projects built directly on Linux as well:
  5. Press “Finish” to generate the project. Now we will try calling a function from the libpng library and will go through most possible missing steps that could cause a build-time or run-time error. Add the following code to the main() function and try building the project:

Источник

opencv undefined reference errors that only occur in linux

I am now building a library that relies on some functions of opencv, and the way how I use opencv is that I link it statically. The built library, however, is a dynamic one (dll.dll in windows and dll.so in linux for example). After having the library, I have no difficult in building a demo program that uses this dynamic library (.dll) in the windows environment. It just works. However,when it was build on Linux. It is a different story. I can build that dynamic library (dll.so), but when I invoke it in the demo program, I have the following errors:

I do not know what I can do in this situation. In fact, I did not call cv::mulSpectrums, cv::getOptimalDFTSize and cv::dft when I built the dynamic library dll.so. Any ideas? Thanks.

1 Answer 1

The problem I have found comes from the library order, and you can check OpenCV undefined references for more details.

Not the answer you’re looking for? Browse other questions tagged linux opencv or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Undefined reference to .. error (Linux) — Compiles fine in OSX

Have wasted almost full 4 days trying to compile this package. It compiles fine in OSX 10.6, but gives Undefined reference errors when I try to compile it on a linux (Kubuntu 10.04, 3.8.0.27 kernel) machine.

The error seem to be that the makefiles are ordered wrong, but AFAIK, I am the only one having trouble compiling it. So I’m trying to find what’s making the difference. The software package is quite big and editing the Makefiles and moving 30-50 libraries here and there doesn’t seem like a good idea.

Here’s the differences I think I found so far

  • Compiler — gcc-4.7 (Linux) and llvm-gcc-4.2 (OSX)
  • Compiler flags —shared (Linux) and -dynamic -dynamiclib -undefined dynamic_lookup (OSX)

Anyone have any suggestions?

I tried using clang++ and llvm-gcc-4.7 as the compiler, but I think it still used the same linker ( ld ?). So I could try to specify to use llvm ? How do I do that?

is —shared flag somehow different from the dynamic -dynamiclib -undefined dynamic_lookup flags in OSX?

Does the linux kernel or distribution matter? (I think they compiled it fine on a CentOS machine)

Please help. Thanks a lot.

2 Answers 2

Compiled it with gcc 4.4 and worked flawlessly. I guess the order doesn’t matter on 4.4 for the given package.

The undefined references type of errors can be caused by a symbol not being compiled in, not being linked or being linked out of order. The way to debug this is to check the linker line, the symbol that the linker complains about. The error message will probably tell you what object file has the dependency.

Now, you need to find out whether the symbol is compiled or linked, for that you will need to find if it is in any of the object files or in any of the libraries and which. You can use the nm command line tool to list the symbols that are defined in any given .o or library. If the symbol is not there, then you need to figure out what to add to the linker line and that will solve it.

If the symbol appears in one library, then identify which of the libraries depends on that symbol (from the linker error message) and the library that contains it. The former must be listed before the latter in the linker command line (assuming static linking).

As a simple hack, although I recommend against it, you can instruct the gcc linker to do multiple passes by using the —start-group and —end-group command line options. Although I really recommend that you figure out the order of dependencies, as that will also give you a better insight into your project.

Источник

C Linking Error: undefined reference to ‘main’

I have read the other answers on this topic, and unfortunately they have not helped me. I am attempting to link several c programs together, and I am getting an error in response:

I have exactly one main function and it is in runexp. The form is

Any thoughts on why I might get this error? Thanks!

4 Answers 4

You should provide output file name after -o option. In your case runexp.o is treated as output file name, not input object file and thus your main function is undefined.

You’re not including the C file that contains main() when compiling, so the linker isn’t seeing it.

You need to add it:

You are overwriting your object file runexp.o by running this command :

In fact, the -o is for the output file. You need to run :

runexp.out will be you binary file.

Generally you compile most .c files in the following way:

gcc foo.c -o foo. It might vary depending on what #includes you used or if you have any external .h files. Generally, when you have a C file, it looks somewhat like the following:

When I get an ‘undefined reference to main’, it usually means that I have a .c file that does not have int main() in the file. If you first learned java, this is an understandable manner of confusion since in Java, your code usually looks like the following:

I would advise looking to see if you have int main() at the top.

Not the answer you’re looking for? Browse other questions tagged c linker or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Читайте также:  Забыл пароль для windows live
Оцените статью