C - undefined reference to - c

I am currently working on a project in C. I searched through multiple similar questions, but the answers are too unspecific for me, because I haven't really worked with C in the past.
My problem is the following:
The project consists of multiple different files. I am editing the file "pm3/armsrc/epa.c". Now i need to use a function from another file. This function is located in "pm3/common/mbedtls/sha1.c.
I tried to include this file in my epa.c like this:
#include "../common/mbedtls/sha1.h"
It seems to work and I could access the function. But after starting the Makefile in order to compile the project (which worked fine until now), i get an error message in epa.c:
undefined reference to `mbedtls_sha1_ret'
In other answers it seems to be a problem with the linker and the gcc call, but the Makefile, which calls multiple other Makefiles in order to build everything are all pre-made. As you can see here in the "make all" output, the file with the needed function is successfully builded:
Compiling mbedtls
cd ../common/mbedtls && make all
make[2]: Entering directory '/pm3/common/mbedtls'
ar rcs libmbedtls.a aes.o asn1parse.o asn1write.o base64.o bignum.o ctr_drbg.o entropy_poll.o entropy.o error.o timing.o ecp.o ecp_curves.o certs.o camellia.o blowfish.o cipher_wrap.o cipher.o cmac.o des.o ecdsa.o md.o md_wrap.o md5.o oid.o pem.o arc4.o pk.o pk_wrap.o pkwrite.o pkcs5.o pkcs12.o pkparse.o platform.o platform_util.o rsa.o rsa_internal.o sha1.o sha256.o sha512.o threading.o x509.o x509_crl.o x509_crt.o
ranlib libmbedtls.a
make[2]: Leaving directory '/pm3/common/mbedtls'
If someone could maybe take a look at the Makefiles or if someone has another possibility in order to get the compilation working, I would be very happy. Here is the link to the git-repo: https://github.com/Proxmark/proxmark3. My knowledge of Makefiles is very low, so I don't see any problems in the pre-made ones.

Without diving into the makefile, you did use the include directive correctly,
but probably the executable's make command (link) did not include the
library you now used (and refernced).
Look for the place in the makefile where there is a call to the linker
(gcc or maybe g++ or ld), and see if the library (probably mbedtls) appears on the
list of files being linked.

Related

arm toolchain does not seem to have C libraries

I am trying to cross compile a simple program with the arm toolchain. And stdio library points to another library, which results in the following error:
/home/sansari/tools/arm-eabi-4.7/bin/arm-eabi-gcc hello.c -o hello
In file included from hello.c:3:0:
/home/sansari/tools/arm-eabi-4.7/bin/../lib/gcc/arm-eabi/4.7/include-fixed/stdio.h:50:23: fatal error: sys/cdefs.h: No such file or directory
compilation terminated.
make: *** [hello] Error 1
At first, I thought this file is in some subdirectory of the toolchain, and I need to include the folder for this library in my makefile. But a quick tree output says that it is not. So given that I have used this toolchain successfully for a larger project, I know it is copied from somewhere. So my question is how do I add it to this project please? And what is the appropriate place to copy it from? I am trying to understand why it is not in the toolchain folders that I have, and how I should add it to my project please.
#Olaf - You have been very helpful; I however have a little more learning curve. But I do understand what you are instructing. I know I have the libraries in my build system since have built for this platform successfully. I even know that it is in my WORKING_DIRECTORY. What I do not know is if I can copy a folder and address the issue entirely or do I need to keep compiling and see what errors I get. That is do it incrementally. For example, one of the files, which was missing was cdefs.h. And I was able to find it in the folder:
/home/sansari/ndk/android-ndk-r10d/platforms/android-19/arch-arm/usr/include/sys/cdefs_elf.h
So I copied it over and the build process proceeded to the next stage. I do like to know if I should perhaps have copied the entire /sys or /include directory or compared the /include directory of my source and make sure all the files are also in the destination directory also. And that way I can avoid having to compile a number of times.
But nevertheless, I proceeded with copying one file at a time. The last error I got is:
/home/sansari/tbt/tools/arm-eabi-4.7/bin/../lib/gcc/arm-eabi/4.7/include/stdint.h:3:26: fatal error: stdint.h: No such file or directory
And I look in the include directory; I see a file call stdint.h
What do you make of that? Does that make sense to you? I am confused by this error. How can I get this error when the file is in the directory. Basically it seems make is saying this file does not exist when it does.
I did find This post also, which seems to say what you are saying. I just need to know what is the best way to address it.
#Olaf - I really appreciate all your help. I was able to get through all the library errors by putting an include statement in my makefile. Now the linker is giving me errors as follows:
/home/sansari/tbt/tools/arm-eabi-4.7/bin/arm-eabi-gcc -I../../ndk/android-ndk-r10d/platforms/android-19/arch-arm/usr/include hello.c -o hello
/home/sansari/tbt/tools/arm-eabi-4.7/bin/../lib/gcc/arm-eabi/4.7/../../../../arm-eabi/bin/ld: cannot find crt0.o: No such file or directory
/home/sansari/tbt/tools/arm-eabi-4.7/bin/../lib/gcc/arm-eabi/4.7/../../../../arm-eabi/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
make: *** [hello] Error 1
update - I searched for a solution for the above errors. The first error is discussed here and -nostartfiles switch seems to work for me for now. I found this link that talks about libc.a being the fix for the error about not finding -lc. So I found an appropriate copy of libc.a and passed the location to the linker. The program compiles, but I get one last warning as follows:
warning: cannot find entry symbol _start; defaulting to 00000000000080dc
For which this link suggested using --entery-main switch. So now I have an executable. I want to thank you again. If you do see any problem in what I have done, please post something for me. it is gonna take a while for me to get this executable on my device.

Multiple Files In One Project - C

Since I am just now learning C I want to be able to create one "Projects" folder in XCode to hold all my mini files that I create to learn different stuff. Such as HelloWorld.c IfElse.c WhleLoop.c however I am having a major issue. I can't run these different main files without getting the error below...
duplicate symbol _main error:
linker command failed with exit code 1 (use -v to see invocation)
What is the easiest way to go about this? I want to only have one project open in Xcode to be able to very easily reference my previous files and just create little tests files that do different learning things before I really get into C.
Can anyone suggest a workaround?
-Henry
The best is to write Makefile
Open one new file with the name "Makefile"
In that file type as given below:
all:hello ifelse while
hello:HelloWorld.o
<tab>cc -o hello HelloWorld.o
ifelse:IfElse.o
<tab>cc -o ifelse IfElse.o
WhileLoop.o:WhileLoop.o
<tab>cc -o while WhileLoop.o
The above given is a makefile which are used for creating multiple executables under a single project in Linux. It is also available in standard sdks like Eclipse. For any new file you want compile add it to the makefile as shown for other files.
Once you finish writing the makefile enter the command make in the terminal.
Everything will get compiled and different executables get created.
To know more about the makefile you can refer to the below link:
http://www.cs.wmich.edu/~sdflemin/instr_pgs/make/index.html

Trying to adapt existing c project to CUDA, .cu files not found by Makefile

I'm trying to accelerate a key function in a c project (not c++) using CUDA.
For some reason, i can't get the Makefile's to recognise the .cu extension when I change the name of one of the files to .cu.
It's using a configure script and .am/.in/.deps files, which I don't really understand all that well, but basically I grepped references to file.c and changed them all to file.cu, but it produces a file.o: File Not Found error.
Top level make file
https://www.dropbox.com/s/g282qvbdu8pdas0/Makefile
Src folder makefile
https://www.dropbox.com/s/b4pq026od8gauqi/Makefile
The search command I used was
grep -R -i "file.c"
and I simply changed them all to file.cu, then re-ran configure, make clean, make all - result is File Not Found.
I suppose it must be something to do with extensions being ignored/accepted by the Makefile, but as it's been a long time since I've programmed in C and I've never used such complex Makefiles I don't know how to fix it.
Any ideas?
*PS Also, file.cu has compile errors at the moment, but the error message I'm getting is File Not Found, so I think that's not the problem.
You need to have a rule to build o file from a cu file:
cudafile.o: cudafile.cu
nvcc $(NVCC_FLAGS) -c %< -o $#
So you also need to specify the rule for the cu file, and use nvcc for compilation.
The following guide seems to cover it...
http://mcclanahoochie.com/blog/2011/02/automake-and-cuda/
Actually, most of the advice given in the link seems unnecessary for basic compilation, but for some reason I found that when I re-created the config file using autoconf it worked. No explanation comes to mind.

Statically linking libclang in C code

I'm trying to write a simple syntax checker for C code using the frontend available in libclang. Due to deployment concerns, I need to be able to statically link all the libraries in libclang, and not pass around the .so file that has all the libraries.
I'm building clang/llvm from source, and in llvm/Release+Asserts/lib I have a bunch of .a files that I think I should be able to use, but it never seems to work (the linker spews out thousands of errors about missing symbols). However, when I compile it using the libclang.so also present in that directory as follows:
clang main.c -o bin/dlc -I../llvm/tools/clang/include -L../llvm/Release+Asserts/lib/ -lclang
Everything seems to work well.
What is the minimum set of .a files I need to include to make this work? I've tried including absolutely all of the .a files in the build output directory, with them provided to clang/gcc in different orders, without any success. I only need the functions mentioned in libclang's Index.h, but there don't seem to be any resources or documentation on what the various libclang*.a files are for. It would be very helpful to know which files libclang.so pulls in.
The following is supposed to work, as long the whole project has all static libraries (I counted 116 in my Release/lib directory).
clang main.c -o bin/dlc -I../llvm/tools/clang/include ../llvm/Release/lib/*.a
[edit: clang main.c -o bin/dlc -I../llvm/tools/clang/include ../llvm/Release/lib/libclang.a ../llvm/Release/lib/*.a]
Note that the output binary is not static, so you don't need any -static flag for gcc or ld, if you're using this syntax.
If that doesn't work you might need to list the libraries in order: if some library requires a function available in another library, then it may be necessary to list it first in the command line. See comments about link order at:
http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Link-Options.html#Link-Options

How to stop make (in makefile) after "No such file or directory" error?

So, most of the times I'm testing if every include is correct on a given C/C++ code, I have a makefile with a gcc/g++ call with proper -I option for searching headers on specific directories (like every program) when I'm compiling sources to headers.
However, if the included directory is not correct and an undefined header appears (e.g. foo.h has #include and was not found), the gcc/g++ will just spit a bunch of errors for every include I have of that foo.h header for all other sources I'm compiling afterwards (and I'm already using -Werror -Wfatal-errors to make gcc/g++ to stop).
So, my question is simple: how can I tell makefile stop after the first error of the type "No such file or directory" it finds? It is really annoying it continue to compile sources and sources, giving me hundreds of errors just for a repeated error I already understood.
It probably continues because you told it to. See the following two options of GNU make:
-k, --keep-going Keep going when some targets can't be made.
-S, --no-keep-going, --stop
Turns off -k.
Put the header files into a variable and use that variable as a dependency. The following snippet will not build anything until the specified headers exist.
HEADERS=test.h other.h /usr/include/special.h
all: $(HEADERS) $(BINPROGS)
[... all other rules go here as usual ...]
*.h:
echo found $#
The ".h:" simply prints out each header that is found before any building even starts. The makefile itself stops if a header cannot be found (and it will stop before trying to compile anything).
I believe that that is what you wanted?
you can write a shell script to check for error conditions before running the make script.

Resources