C compilation problem - c

I am learning how to use cygwin for alchemy. I created a test library celconv.h which is there inside the folder celconv. I want to use that header file in my c code.
When i use #include <celconv/celconv.h> the compiler gives an error "No such file or directory"
i compile the code like this:
gcc -c test.c
Test.c inside a folder named test
#include <stdio.h>
#include <celconv/celconv.h>
int main()
{
float UserInput;
printf("Enter a temperature in celsius to convert to fahrenheit:");
scanf("%f",&UserInput);
celconvert(UserInput);
return 0;
}
celconv.h inside a folder celconv which is inside test folder:
extern void celconv(float Cel);
celconv.c:
#include<stdio.h>
#include"celconv.h"
void celconvert(float Cel)
{
float Fah;
Fah = Cel * 9/5 + 32;
printf("%f",Fah);
}

Is the path you are giving correct? Be careful about using < > and " ". The first tells the compiler "search for include files in your include folder", while the second says "look into source files directory".

If the "celconv" directory is in the current directory, the best way is probably use #include "celconv/celconv.h" syntax. If it is somewhere else, then do this:
gcc -I/path/to/wherever/celconv/directory/is -c test.c
Note that it's not the path to the celconv directory itself, but to the containing directory!
As mentioned in the comment, this approach will work for the current directory too if you don't want to use "celconv/celconv.h" for whatever reason:
gcc -I. -c test.c

Include files in the local directory need to be referenced using the
#include "file.h"
syntax. The angle brackets do not look into the current directory.

Related

Linking files / header files in C

I have 2 c files (& their header files). I have included the function "put" in the corresponding header, but I still have the following errors, when I input "gcc -o main main.c" in the terminal.
main.c:(.text+0x389): undefined reference to `put' collect2: error: ld
returned 1 exit status
may I know the reason? How should I modify my code?
I tried to change the linking order in makefile but failed. Any advice is appreciated, thanks!
CMakeLists.txt
cmake_minimum_required(VERSION 3.19)
project(Demo)
set(CMAKE_CXX_STANDARD 14)
include_directories(.)
add_executable(Demo
main.c main.h KeyValueStore.c KeyValueStore.h )
main.c
#include "main.h"
...
int main() {
...
if (strcmp("PUT", tokens[0]) == 0) {
put(tokens[1], tokens[2]);
...
}
main.h
...
#include "KeyValueStore.h"
...
KeyValueStore.c
#include "KeyValueStore.h"
#define BUFSIZE 1024
typedef struct KeyValueStore {
char key[BUFSIZE];
char value[BUFSIZE];
} KV_Store;
KV_Store kvStore[BUFSIZE];
...
int put(char* key, char* value){
...
}
KeyValueStore.h
...
typedef struct KeyValueStore;
int put(char* key, char* value);
...
Check to see if you have any .o files in that folder and delete them if you do. It's possible the compiler failed at some point while compiling which left *.o files that aren't linked properly
EDIT: I misread the question because for some reason it came up as a c++17 question for me. I'm not sure if what I said still applies to C though I do know it works with C++. Sorry about that to everyone that read my answer before I edited it
Not 100% sure if this is why you are having the error, though you need to put all .c files in the compiler.
So you currently are trying to "gcc -o main main.c" where instead you want to do something more like "gcc -o main main.c keyValueStore.c".
If you do not give the compiler every .c file, it won't have all the definitions and you will get an error similar to what you have.
I also don't really think you need main.h, assuming there isn't any more code in there, it really isn't worth having a whole extra file and instead just putting the #include in main.c.

gcc cannot find function in lib

I am trying to make a C program using libmbus, which I have installed on my raspberry pi. In my /usr/lib directory I have the file libmbus.so and in my /usr/include directory I have the the file ./mbus/mbus.h.
The program looks like this:
#include <stdio.h>
#include <mbus/mbus.h>
int main(void)
{
mbus_handle* MbusHandle;
MbusHandle = mbus_connect_serial("/dev/ttyS1");
return 0;
}
When I try to run "gcc main.cpp -lmbus" I get:
main.cpp:(.text+0xe): undefined reference to `mbus_connect_serial(char const*)'
I tried to run
nm -D /usr/lib/libmbus.so
which among others gives
00009930 T mbus_connect_serial
So it appears that the function mbus_connect_serial is part of libmbus.so.
In the header file the function mbus_connect_serial is defined like this:
mbus_handle * mbus_connect_serial(const char * device);
I can't seem to figure out what is wrong. Can anyone guide me in the right direction?
If you're really trying to create a c program, rename main.cpp to main.c

is it possible to indicate a flag to include an audio files directory in Makefile for gcc?

I would like to know if it's possible to indicate for gcc some flag that include a directory for different kind of files, like .wav or .txt. Then in my code I wouldn't need to indicate the directory to read the file.
Pass it as a macro on the compile line, e.g.:
#include <stdio.h>
#define qstr(a) str(a)
#define str(a) #a
int main(void)
{
printf("The path is %s\n", qstr(MY_FILE_PATH));
return 0;
}
Then compile as:
gcc -DMY_FILE_PATH="`pwd`/mymediadir/mywavfilesdir" test.c -o test
When run gives:
./test
The path is /home/joe/tmp/mymediadir/mywavfilesdir

how to use generated code from matlab

I want to use the C-coder in Matlab. This translates an m-code to C-code.
I use a simple function that adds 5 numbers.
When the code is generated there are a lot of C- and H-files.
of course you could just pick the code you need and import it in your code, but that's not the point of this exercise, as this will no longer be possible when the matlab-code will get more difficult.
Matlab delivers a main.c file and a .mk file.
/* Include Files */
#include "rt_nonfinite.h"
#include "som.h"
#include "main.h"
#include "som_terminate.h"
#include "som_initialize.h"
//Declare all the functions
int main(int argc, const char * const argv[]){
(void)argc;
(void)argv;
float x1=10;
float x2=20;
float x3=30;
float x4=40;
float x5=50;
float result;
/* Initialize the application.
You do not need to do this more than one time. */
som_initialize();
main_som();
result=som(x1,x2,x3,x4,x5);
printf("%f", result);
som_terminate();
return 0;
}
When I run this on a raspberry-pi with
gcc -o test1 main.c
It gives me undefined references to all the functions...
Any ideas what went wrong?
You have to build it with the generated makefile (the mk file) so it links with the correct Matlab libraries - that's where those functions are defined:
$ make -f test.mk
You also need to compile the other C files along with your main.c. If main.c is in the same directory as the generated code, you should be able to just do:
gcc -o test1 *.c
If the generated code is in another directory, then you can do something like:
gcc -o test1 /path/to/code/*.c -I/path/to/code main.c

Compiling with Mingw

I have a bunch of C files and header files in the folder. When I compile the C files with MinGW compiler, it shows that there is no such file or directory. But I have all the files in the same folder. How do I get them to compile?
I have attached the code for your reference (file computil.c):
#include <stdio.h>
#include <computil.h>
#include <dataio.h>
int getc_skip_marker_segment(const unsigned short marker, unsigned char **cbufptr, unsigned char *ebufptr)
{
int ret;
unsigned short length;
ret = getc_ushort(&length, cbufptr, ebufptr);
if(ret)return(ret);
length -= 2;
if(((*cbufptr)+length) >= ebufptr)
{
fprintf(stderr, "ERROR : getc_skip_marker_segment : ");
fprintf(stderr, "unexpected end of buffer when parsing ");
fprintf(stderr, "marker %d segment of length %d\n", marker, length);
return(-2); }(*cbufptr) += length; return(0);
}
}
I am compiling it with gcc -c computil.c.
I believe you are going to have to add the current directory to the list of "standard places" that gcc uses. When you use instead of "computil.h", a Unix-style compiler won't look in the current directory.
For a quick fix to that, add -I. to the gcc command line. (dash, capital eye, period):
gcc -I. computil.c
If that's an application include file intended to be found where the source files are found, then you should change the include line to:
#include "computil.h"
That's one of the valuable nuances from Classic C that got lost in the ANSI standardization process. Standard C lets the compiler decide if there's a difference or between <> bracketed and "" quoted headers. It makes a difference in Unix and GNU ("GNU's Not Unix!"), well, pretty much is Unix only better in places.
To put it simple, #include <header.h> means "search in the compiler's own library directories, while #include "header.h means "search in the same directory as the .c file that made the #include".
I don't believe gcc has any library headers named computil.h and dataio.h, so the code won't compile.

Resources