Compiler can't find installed library headers - c

Writing my first application in C and it can't seem to find the header file (which I installed following the instructions in the readme):
#include <stdio.h>
#include <stdlib.h>
#include <ftdi.h>
int main (int argc, char *argv[])
{
int ret;
struct ftdi_context = *ftdi;
struct ftdi_version_info version;
if ((ftdi = ftdi_new()) == 0)
{
fprintf(stderr, "ftdi_new failed\n");
}
else {
fprintf(stderr, "Success\n");
}
return 0;
}
But it finds it here:
ollieb#ursus ~/Documents/BitBang $ locate ftdi.h
/home/ollieb/.local/share/Trash/files/libftdi/libftdi/ftdipp/ftdi.hpp
/home/ollieb/.local/share/Trash/files/libftdi/libftdi/src/ftdi.h
/home/ollieb/Applications/libftdi1-1.3/build/doc/html/group__libftdi.html
/home/ollieb/Applications/libftdi1-1.3/build/doc/man/man3/ftdi.h.3
/home/ollieb/Applications/libftdi1-1.3/build/doc/man/man3/ftdi.hpp.3
/home/ollieb/Applications/libftdi1-1.3/ftdipp/ftdi.hpp
/home/ollieb/Applications/libftdi1-1.3/src/ftdi.h
/usr/include/libftdi1/ftdi.h
/usr/include/libftdi1/ftdi.hpp
This is what happens when I try to compile, it says the file cannot be found (when I run examples in the code library folder they run fine):
ollieb#ursus ~/Documents/BitBang $ make
make bitbang
make[1]: Entering directory '/home/ollieb/Documents/BitBang'
cc -Wall -g bitbang.c -o bitbang
bitbang.c:3:18: fatal error: ftdi.h: No such file or directory
compilation terminated.
<builtin>: recipe for target 'bitbang' failed
make[1]: *** [bitbang] Error 1
make[1]: Leaving directory '/home/ollieb/Documents/BitBang'
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2

By default, compiler searches for the header files in the default INCLUDE path, not the whole filesystem.
If you have your headers in a custom path, you need to inform the compiler about that. For example, with gcc and clang, you can use the -I switch to let the compiler know the path where the header files are present.
From gcc online manual,
-Idir
Add the directory dir to the head of the list of directories to be searched for header files. [...]

Related

How to add Compiler Flag in CMakeLists Clion for shm_open [duplicate]

This question already has answers here:
undefined reference to `shm_open' using CMake
(2 answers)
Closed 1 year ago.
Simple Program:
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
int main() {
int shm_fd;
shm_fd=shm_open("sh",O_CREAT|O_RDWR,0666);
return 0;
}
The error is:
Warnung: undefined reference to »shm_open«
collect2: error: ld returned 1 exit status
CMakeFiles/Share_Memory_Project.dir/build.make:102: recipe for target 'Share_Memory_Project' failed
make[3]: *** [Share_Memory_Project] Error 1
CMakeFiles/Makefile2:94: recipe for target 'CMakeFiles/Share_Memory_Project.dir/all' failed
make[2]: *** [CMakeFiles/Share_Memory_Project.dir/all] Error 2
CMakeFiles/Makefile2:101: recipe for target 'CMakeFiles/Share_Memory_Project.dir/rule' failed
make[1]: *** [CMakeFiles/Share_Memory_Project.dir/rule] Error 2
Makefile:137: recipe for target 'Share_Memory_Project' failed
make: *** [Share_Memory_Project] Error 2
To fix this Problem, I have to add a Compiler Flag:
-lrt at the end. Like this: gcc main.c -o main -lrt. So When I put this in Command Line at Ubuntu Terminal it compile fine.
My Question is: How to add this Flag at my CMakeLists.txt in Clion?
How my file currently looks:
cmake_minimum_required(VERSION 3.19)
project(Share_Memory_Project C)
set(CMAKE_C_STANDARD 99)
add_executable(Share_Memory_Project main.c)
To link a target with a library, use target_link_libraries. You would:
add_executable(Share_Memory_Project main.c)
target_link_libraries(Share_Memory_Project PUBLIC rt)

How to fix 'undefined reference' when compiling in C?

I'm trying to compile a C program linking two previously created object files but keep getting an 'undefined reference' error.
I'm using Visual Code to write the code and Ubuntu on Windows to compile using a makefile. The two C files, task5.c and reverse.c which have been made into object files both contain #include reverse.h statements which contains the prototypes for the functions in reverse.c.
task5.c
#include <stdio.h>
#include "ctap.h"
#include "reverse.h"
TESTS {
const char *a = "Family";
char *b = reverse(a);
//test 1
ok(string_length(a) == string_length(b), "Strings are the same size");
//test 2
is("ylimaF", b, "Strings match");
}
reverse.c
#include <stdio.h>
#include <stdlib.h>
#include "reverse.h"
char *reverse(const char *str) {
//code
}
int string_length(const char *str) {
//code
}
reverse.h
char *reverse(const char *str);
int string_length(const char *str);
makefile
linked:
gcc -o linked task5.o reverse.o
task5.o
gcc -c -o task5.o task5.c
reverse.o
gcc -c -o reverse.o reverse.c
When I run the command make linked I expect it to be made and return nothing.
But when I run that command I get this error:
cc task5.o -o linked
task5.o: In function `ctap_tests':
task5.c:(.text+0x1abe): undefined reference to `reverse'
task5.c:(.text+0x1ace): undefined reference to `string_length'
task5.c:(.text+0x1adc): undefined reference to `string_length'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'linked' failed
make: *** [task5] Error 1
According to this GNU make documentation, the GNU make program will try to use GNUMakefile, makefile or Makefile.
The make program will not try makefile.mk, which means that e.g. make linked will use no makefile and only the default rules.
You can solve this by either renaming your makefile as Makefile (the most common) or makefile; Or by using the -f option to specify the makefile
$ make -f makefile.mk linked

MinGW compilation says header files are missing, which package maybe missing?

This is the first gcc error on compilation, copying features.h from the web into the include folder for MinGW helped :
$ gcc -g hello_world.c
In file included from c:\mingw\include\stdio.h:55,
from hello_world.c:1:
c:\mingw\include\_mingw.h:174:10: fatal error: features.h: No such file or directory
#include <features.h>
^~~~~~~~~~~~
compilation terminated.
This is the new error :
$ gcc -g hello_world.c
In file included from c:\mingw\include\_mingw.h:174,
from c:\mingw\include\stdio.h:55,
from hello_world.c:1:
c:\mingw\include\features.h:406:10: fatal error: stdc-predef.h: No such file or directory
#include <stdc-predef.h>
^~~~~~~~~~~~~~~
compilation terminated.
The hello-world file :
#include <stdio.h> int main(int argc, const char *argv[]) {
/* I am C developer */
printf("Hello, Poftut! \n");
return ; }
C:\MinGW\bin\ is in PATH.Pretty sure I have missed some package installation.

I've included a header file but I still get undefined when trying to run make

I am trying to implement bcrypt into an open source project I've found, more of a learning thing. I've git cloned https://github.com/rg3/bcrypt and moved the bcrypt.h bcrypt.c and crypto_blowfish into my projects file, then in my file database.c I've
include "bcrypt.h"
Then when I run make I get:
database.c:2084: undefined reference to `bcrypt_checkpw'
This is how I'm using it
int ret;
ret = bcrypt_checkpw(pass, row[0]);
assert(ret != -1);
if (ret == 0) {
return 1;
} else {
return 2;
}
I have a Makefile I'm not sure if I did this right but I added bcrypt.h to the makefile
.obj/database.o: database.c bcrypt.h server.h log.h create.h player.h sleep.h tool.h drdata.h drvlib.h timer.h direction.h map.h mem.h database.h misc_ppd.h badip.h
All compile
gcc -O -g -m32 -rdynamic -L/usr/lib/mysql -o server .obj/server.o .obj/io.o .obj/libload.o .obj/tool.o .obj/sleep.o .obj/log.o .obj/create.o .obj/notify.o .obj/skill.o .obj/do.o .obj/act.o .obj/player.o .obj/rdtsc.o .obj/los.o .obj/light.o .obj/map.o .obj/path.o .obj/error.o .obj/talk.o .obj/drdata.o .obj/death.o .obj/database.o .obj/see.o .obj/drvlib.o .obj/timer.o .obj/expire.o .obj/effect.o .obj/command.o .obj/date.o .obj/container.o .obj/store.o .obj/mem.o .obj/sector.o .obj/chat.o .obj/statistics.o .obj/mail.o .obj/player_driver.o .obj/clan.o .obj/lookup.o .obj/area.o .obj/task.o .obj/punish.o .obj/depot.o .obj/prof.o .obj/motd.o .obj/ignore.o .obj/tell.o .obj/clanlog.o .obj/respawn.o .obj/poison.o .obj/swear.o .obj/lab.o .obj/consistency.o .obj/btrace.o .obj/club.o .obj/teufel_pk.o .obj/questlog.o .obj/badip.o -lmysqlclient -lm -lz -ldl -lpthread
.obj/database.o: In function `load_char_pwd':
/home/ec2-user/astonia3_server/database.c:2084: undefined reference to `bcrypt_checkpw'
collect2: error: ld returned 1 exit status
make: *** [server] Error 1
The bcrypt_checkpw in .h file is put on extern Cso it has C linkage
The bcrypt_checkpw in .c file has C++ linkage
They are different symbols so you will get linking error.

C makefile undefined-reference error

I am having slight problems with using makefile in C. Ive been following a tutorial in a textbook, but it doesnt seem to want to work. I have three files, message_hider.c, encrypt.h and encrypt.c. When I create a makefile for these files it returns an error, but when I run each command individually it works just fine. Here are my files.
encrypt.c
#include "encrypt.h"
void encrypt(char *message) {
char c;
while (*message) {
*message = *message ^ 31;
message++;
}
}
message_hider.c
#include <stdio.h>
#include "encrypt.h"
int main() {
char msg[80];
while (fgets(msg, 80, stdin)) {
encrypt(msg);
printf("%s", msg);
}
}
encrypt.h
void encrypt(char *message);
Makefile
message_hider: message_hider.o encrypt.o
gcc message_hider.o encrypt.o -o message_hider
message_hider.o: message_hider.c encrypt.h
gcc -c message_hider.c
encrypt.o: encrypt.c encrypt.h
gcc -c encrypt.c
Error message
$ make message_hider
cc message_hider.o -o message_hider
message_hider.o:message_hider.c:(.text+0x17): undefined reference to `encrypt'
message_hider.o:message_hider.c:(.text+0x17): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `encrypt'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../x86_64-pc-cygwin/bin/ld: message_hider.o: bad reloc address 0x0 in section `.pdata'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../x86_64-pc-cygwin/bin/ld: final link failed: Invalid operation
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'message_hider' failed
make: *** [message_hider] Error 1
$ make message_hider
cc message_hider.o -o message_hider
That is not the rule you've specified in your makefile. First off, it appears to be using cc rather than gcc. Second, there's no mention of encrypt.o in there which is why your link is failing.
Try to explicitly use the makefile, such as with:
make -f Makefile message_hider
It may be that it's picking up a different makefile, one that either has different rules or one that simply relies on the default rules like .c.o.
And, based on your update that:
make -f Makefile message_hider
gives you:
$ make -f Makefile message_hider
make: Makefile: No such file or directory
make: *** No rule to make target 'Makefile'. Stop.
that's the error you get when Makefile does not actually exist.
So you need to check that it's in the current directory, and named exactly as you expect.
That would explain the use of default rules as mentioned earlier, since your makefile isn't actually being picked up.
Another thing to check, though it's probably moot now that we've seen the error above, is that you're actually running the correct make program. Use which make to find out where it is (should be /usr/bin/make on CygWin) and make --version to check the version.

Resources