I'm working on a project (hangman.c) and I want to include a module I wrote from another directory (quicksort.h). Here is the top of my hangman.c file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../quicksort/quicksort.h"
I've been running this command:
clang -o hangman hangman.c
And i get this error message:
/usr/bin/ld: /tmp/hangman-3292ab.o: in function `append_and_order':
hangman.c:(.text+0x26c): undefined reference to `quicksort'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I compile them separately and link them together manually with
gcc -c hangman.c
gcc -c ../quicksort/quicksort.c
gcc -o hangman hangman.o quicksort.o
(As corrected). And that works, but isn't the point of #include to do this automatically at the preprocessing stage?
Related
I keep getting this error when compiling!
Undefined symbols for architecture x86_64:
"_lex", referenced from:
_main in main-7a45f7.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 am compiling a file main.c. Here are the first few lines of the program:
#include <stdio.h>
#include "lex.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
I thought compiling the header file "lex.h" might have been the issue, but I was apparently wrong. I ran the command
gcc -c lex.c lex.o
and received the following warning (but no errors):
clang: warning: lex.o: 'linker' input unused [-Wunused-command-line-argument]
I think I have included all the information needed, but these are the files in my current directory (as per ls):
lex.c lex.h lex.o main.c
Any help would be appreciated, thanks!
Thanks to Tom Karzes and Itagaki Fumihiko for help. The fatal flaw in my compilation was not linking main.o and lex.o. To compile the files properly, this is what you should actually do:
gcc -c lex.c
gcc -c main.c
gcc main.o lex.o
I want to compile mpi with my own libraries. And I'm not sure if the options that work on gcc such as -I/ -L/ are available for mpicc.
I'm trying to compile with the following options but I get the following error.
mpicc -I$(CURRENT_DIR)/util -I$(CURRENT_DIR) -L$(CURRENT_DIR)/util -o server server.c
mpicc -I./util -I. -L./util -o server server.c
/tmp/ccA5be6Z.o: En la función `main':
server.c:(.text+0x195): undefined reference to `list_create'
server.c:(.text+0x219): undefined reference to `list_add'
server.c:(.text+0x228): undefined reference to `list_count'
collect2: error: ld returned 1 exit status
Those are my server.c includes
#include <mpi.h>
#include <list.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
And this is my pwd/util folder
$ ls
list.c list.h list.o
...
mpicc (and the other MPI build commands) are only wrappers on top of your compilers. So if you are using gcc, all the gcc options and directives are available.
Concerning OpenMPI, you can exhibit the compiler and the options used by using the --showme option. You can see the details in the OpenMPI FAQ (https://www.open-mpi.org/faq/?category=mpi-apps#wrapper-showme-with-no-file). Similar options are available in MPICH (https://www.mpich.org/static/docs/v3.2.x/www1/mpicc.html)
Your issue here is not related to MPI. You are just missing adding some objects (probably list.o) in the linking step. You should consider creating a Makefile to ease the build process.
I'm using Eclipse and OpenCV (Version 3) on Ubuntu 15.10 to write a C program but I don't get why I'm always getting the error
undefined reference to symbol 'cvSaveImage'
If I run
pkg-config opencv --cflags --libs
I get
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib
-lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres
-lopencv_videostab -lopencv_calib3d -lopencv_features2d
-lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video
-lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann
-lopencv_core -lopencv_hal
So I added the LIbraries to the GCC C Liker as in the image below
If I don't try to use the function cvSaveImage the program runs, so the other libraries correctly work. I included the highgui library:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdbool.h>
#include <time.h>
Any idea?
cvSaveImage belongs to opencv_imgcodecs.
You are missing opencv_imgcodecs in the libraries (-l).
I wrote a function for a class and I need to submit it so it works when compiled.
main.c header looks like...
#include <stdio.h>
#include <stdlib.h>
my function code fun.c looks like this...
#include <stdio.h>
#include <stdlib.h>
#include "main.c"
yet for some reason they're not linking together. When I compile and run I don't get an error, it just runs main.c as if fun.c doesn't exist, meaning it ignores the function call to fun.c and runs the program without executing what I need to display.
I wrote the code together in main.c, so I know the function works. I'm just having a little trouble splitting it up into two separate .c files and linking them.
thanks for any help
//edit & updates
compiling with gcc -m32 -O1 *.c the two files in the directory are fun.c and main.c
Basically we're submitting a function that has to work with the main.c that is provided but we're not aloud to modify the main.c so i'm not sure how to add it to my function so they both compile/link together
Please use other names for your outsource files in future, main.c is inconvenient. The right way to do it is: Create a header file for your outsource main.c. It should be named main.h. There you will declare your functions for instance: addNumbers(int a, int b); Then you open your outsource main.c file and there you #include "main.h". Open then your fun.c file and #include "main.h". After everything has been done right, compile your main file: gcc -c -o fun.o fun.c » Then your outsource file: gcc -c -o main.o main.c » Now link the two files together: gcc -o name main.o fun.o Then you are able to execute your program.
I'm trying to compile mphidflash on Debian (Jessie) after re-writing the low-level USB interface to talk to libhidapi-libusb instead of libhid (which is no longer supported on Debian) but I'm getting some compiler errors that I'm not sure what do to with.
These are my includes in usb-linux.c:
#include <stdio.h>
#include <usb.h>
#include <hidapi/hidapi.h>
#include "mphidflash.h"
#include <errno.h>
and here are the commands I've tried which error:
$ gcc main.o hex.o usb-linux.o -lusb -lhidapi-libusb -o mphidflash
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/libhidapi-libusb.so: undefined reference to `libusb_handle_events_completed'
collect2: error: ld returned 1 exit status
$ gcc main.o hex.o usb-linux.o /usr/lib/x86_64-linux-gnu/libhidapi-libusb.a -lusb -lhidapi-libusb -o mphidflash
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libhidapi-libusb.a(hid.o): undefined reference to symbol 'libusb_detach_kernel_driver'
//usr/local/lib/libusb-1.0.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Basically, I'm getting an undefined reference to symbol 'libusb_detach_kernel_driver' which suggests that I'm including the libusb library incorrectly but I've no idea what else to try.
Turns out hidapi-libusb uses libusb-1.0 and not libusb-0.1 (both of which can be installed on Debian).
I changed the includes to be:
#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <hidapi/hidapi.h>
#include "mphidflash.h"
#include <errno.h>
And compiled with:
$ gcc main.o hex.o usb-linux.o -lusb-1.0 -lhidapi-libusb -o mphidflash
I'll be putting my changes into a git repo and I'll post the link when I'm finished.