Linking with GHC - c

I am following this short tutorial
and trying to compile Webots application (written in C) using GHC (Glasgow Haskell Compiler, Version 7.10.1) with following command:
ghc --make -no-hs-main -optc-O
-I"/Applications/Webots/include/controller/c/"
-L"/Applications/Webots/lib/"
-outputdir build/release/
advanced_genetic_algorithm_supervisor.c
Safe
-o advanced_genetic_algorithm_supervisor
and getting the following error during the linking phase:
Linking advanced_genetic_algorithm_supervisor ...
Undefined symbols for architecture x86_64:
"_wb_display_draw_text", referenced from:
_main in advanced_genetic_algorithm_supervisor.o
"_wb_display_get_height", referenced from:
_main in advanced_genetic_algorithm_supervisor.o
"_wb_display_get_width", referenced from:
_main in advanced_genetic_algorithm_supervisor.o
"_wb_emitter_send", referenced from:
_evaluate_genotype in advanced_genetic_algorithm_supervisor.o
"_wb_receiver_enable", referenced from:
_main in advanced_genetic_algorithm_supervisor.o
"_wb_receiver_get_data", referenced from:
_check_for_slaves_data in advanced_genetic_algorithm_supervisor.o
"_wb_receiver_get_queue_length", referenced from:
_check_for_slaves_data in advanced_genetic_algorithm_supervisor.o
"_wb_receiver_next_packet", referenced from:
_check_for_slaves_data in advanced_genetic_algorithm_supervisor.o
"_wb_robot_cleanup", referenced from:
_run_optimization in advanced_genetic_algorithm_supervisor.o
"_wb_robot_get_basic_time_step", referenced from:
_main in advanced_genetic_algorithm_supervisor.o
"_wb_robot_get_device", referenced from:
_main in advanced_genetic_algorithm_supervisor.o
"_wb_robot_init", referenced from:
_main in advanced_genetic_algorithm_supervisor.o
"_wb_robot_step", referenced from:
_run_seconds in advanced_genetic_algorithm_supervisor.o
_evaluate_genotype in advanced_genetic_algorithm_supervisor.o
"_wb_supervisor_field_get_sf_rotation", referenced from:
_main in advanced_genetic_algorithm_supervisor.o
"_wb_supervisor_field_get_sf_vec3f", referenced from:
_main in advanced_genetic_algorithm_supervisor.o
"_wb_supervisor_field_set_sf_rotation", referenced from:
_evaluate_genotype in advanced_genetic_algorithm_supervisor.o
"_wb_supervisor_field_set_sf_vec3f", referenced from:
_evaluate_genotype in advanced_genetic_algorithm_supervisor.o
"_wb_supervisor_node_get_field", referenced from:
_main in advanced_genetic_algorithm_supervisor.o
"_wb_supervisor_node_get_from_def", referenced from:
_main in advanced_genetic_algorithm_supervisor.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I believe that it means that the compiler cannot find functions ('symbols'), although it managed to pass through compilation process parsing all includes specified in the program using -I path

It means the linker can't find those functions/symbols, because you didn't tell it to link with the Webots library.
You need to add -lsomething, assuming the library file name is /Applications/Webots/lib/libsomething.dylib (or libsomething.a).

Related

Why doesn't gcc link an SDL 2.0 C project? (macOS) [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
How to use SDL with gcc?
(1 answer)
Closed 3 years ago.
I wrote a simple Chip-8 emulator in C (mostly taking inspiration from this; to be honest, just rewriting it in C). It uses SDL 2.0, which I definitely have installed.
As I tried compiling the files (gcc main.c chip8.c -o chip8), I got this stack of errors:
Undefined symbols for architecture x86_64:
"_SDL_CreateRenderer", referenced from:
_main in main-638d2a.o
"_SDL_CreateTexture", referenced from:
_main in main-638d2a.o
"_SDL_CreateWindow", referenced from:
_main in main-638d2a.o
"_SDL_GetError", referenced from:
_main in main-638d2a.o
"_SDL_Init", referenced from:
_main in main-638d2a.o
"_SDL_PollEvent", referenced from:
_main in main-638d2a.o
"_SDL_RenderClear", referenced from:
_main in main-638d2a.o
"_SDL_RenderCopy", referenced from:
_main in main-638d2a.o
"_SDL_RenderPresent", referenced from:
_main in main-638d2a.o
"_SDL_RenderSetLogicalSize", referenced from:
_main in main-638d2a.o
"_SDL_UpdateTexture", referenced from:
_main in main-638d2a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'm very sure I used the correct #include (#include "SDL2/SDL.h"). This is my project structure:
chip-8
—— main.c
–– chip8.c
–– chip8.h
–– INVADERS (rom file)
Why doesn't the linker work with this? Are any other compiler flags required?
Why doesn't the linker work with this? Are any other compiler flags required?
Yes, you need to tell the linker which libraries to link against, e.g.:
-lSDL2
See How to use SDL with gcc?

How do I compile the Sundown Markdown Renderer example? Keeps returning errors

Sundown a (formerly) popular Markdown editor includes an examples directory, and I want to play around with it on the command line, but I'm having difficulties.
I navigate to examples, I run gcc sundown.c -o sundown and it complains that I don't have markdown.h. So I dump the contents of the src directory and the html directory into the examples category, and run it.
I then get:
Undefined symbols for architecture x86_64:
"_bufgrow", referenced from:
_main in sundown-3bac08.o
"_bufnew", referenced from:
_main in sundown-3bac08.o
"_bufrelease", referenced from:
_main in sundown-3bac08.o
"_sd_markdown_free", referenced from:
_main in sundown-3bac08.o
"_sd_markdown_new", referenced from:
_main in sundown-3bac08.o
"_sd_markdown_render", referenced from:
_main in sundown-3bac08.o
"_sdhtml_renderer", referenced from:
_main in sundown-3bac08.o
What exactly am I doing wrong here? I feel like this should be relatively simple, I'm just at wits end trying to figure out what I'm doing wrong.
you tell gcc where to obtain #include files by adding the parameter:
'-Ipathtoincludes'
you tell gcc where to obtain the needed libraries by adding the parameters:
'-Lpathtoolibrary' and '-lshortlibraryname
note: the -l is lower case 'L'
The library path and library name parameters must be last in the gcc command line, as the linker looks at the linker parameters in the order listed on the command line

stprtool box matlab mex

I am trying to use the kernel function of stprtool box in Matlab. As it is a c file, I used the mex function to link it. However, I encountered the following errors:
Undefined symbols for architecture x86_64:
"_arg1", referenced from:
_mexFunction in kernel.o
"_dataA", referenced from:
_mexFunction in kernel.o
"_dataB", referenced from:
_mexFunction in kernel.o
"_dim", referenced from:
_mexFunction in kernel.o
"_ker", referenced from:
_mexFunction in kernel.o
"_kernel", referenced from:
_mexFunction in kernel.o
"_kernel_id", referenced from:
_mexFunction in kernel.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
mex: link of ' "kernel.mexmaci64"' failed.
arg1 to kernel_id are some variables in the kernel.c. Would you have any idea to fix this problem?

Compile simple hello world ZeroMQ C example, compile flags?

Trying to compile the example hello_world.c from the zeromq tutorial:
http://zguide.zeromq.org/page:all#Ask-and-Ye-Shall-Receive
Pretty sure I have everything installed in OSX Mountain Lion.
clang -Wall hwserver.c -o hwserver
gives me an error:
Undefined symbols for architecture x86_64:
"_zmq_bind", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_ctx_new", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_msg_close", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_msg_data", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_msg_init", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_msg_init_size", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_msg_recv", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_msg_send", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_socket", referenced from:
_main in hwserver-OgrEe6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'm not the most experienced in C. Not sure if I sure be adding a compiler flag for the zmq dylib or headers or my $PATH being off.
in /usr/local/lib:
libzmq.3.dylib
libzmq.a
libzmq.dylib
libzmq.la
and in /usr/local/include:
zmq.h
zmq_utils.h
and echo $PATH:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/lib/:/usr/local/include/
You list the ZeroMQ libraries in your question, but you do not actually link with them. Change the command line to this:
clang -Wall hwserver.c -o hwserver -L/usr/local/lib -lzmq
Explanation of the extra arguments:
-L/usr/local/lib tells the linker to add a path (/usr/local/lib) to the library search path.
-lzmq tells the library to link with the zmq library.
The $PATH environment variable have nothing to do with this, it just tells the shell where to look for commands.

How to include hdf5 header files in a C program on Mac OS X?

I am trying to learn how to program in C, and I want to be able to import data into my C program from a .hdf file.
I am using a mid 2009 MacBook Pro with Mac OS X lion.
I think I am having problems, because I didn't know where on my system to put all the header files associated with hdd (remember, I'm a bit of a noob). So I just downloaded the hdf5-1.8.9-freebsd-shared folder from the hdd website, and manually dragged all the .h files from the /include subdirectory into the /usr/include directory onto my computer.
When I try to compile my .c program using the normal gcc terminal command, I get an error which is along the lines of (sorry about the formatting but I think the exact error doesn't matter too much anyway):
Undefined symbols for architecture x86_64:
"_H5check_version", referenced from:
_main in cc9FVO6S.o
"_H5Fcreate", referenced from:
_main in cc9FVO6S.o
"_H5Screate_simple", referenced from:
_main in cc9FVO6S.o
"_H5open", referenced from:
_main in cc9FVO6S.o
"_H5T_STD_I32LE_g", referenced from:
_main in cc9FVO6S.o
"_H5Dcreate2", referenced from:
_main in cc9FVO6S.o
"_H5T_NATIVE_INT_g", referenced from:
_main in cc9FVO6S.o
"_H5Dwrite", referenced from:
_main in cc9FVO6S.o
"_H5Dclose", referenced from:
_main in cc9FVO6S.o
"_H5Sclose", referenced from:
_main in cc9FVO6S.o
"_H5Fclose", referenced from:
_main in cc9FVO6S.o
"_H5Fopen", referenced from:
_main in cc9FVO6S.o
"_H5Dopen2", referenced from:
_main in cc9FVO6S.o
"_H5Dread", referenced from:
_main in cc9FVO6S.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Though the exact error varies depending on exactly what code I am trying to compile. This was generated when I tried to compile a sample program.
Does anybody have any experience with getting hdf5 to work in C in Mac OS X? I have found it all very confusing.
By the way, I normally use python, and frequently use h5py without trouble.
You have to tell your linker when you are using a library using -l<library-name> and -L<library-dir> for non-standard locations.
If you installed hdf5 in /usr/local/hdf5, you need something like:
gcc -o h5ex_d_rdwr h5ex_d_rdwr.c -I/usr/local/hdf5/include -L/usr/local/hdf5/lib -lhdf5
If you are using the High-Level API, also add -lhdf5_hl.
If you have pkg-config installed and it is aware of your hdf5 installation, you can let it do it for you:
gcc -o h5ex_d_rdwr h5ex_d_rdwr.c `pkg-config hdf5 --cflags --libs`

Resources