I am trying to compile the following multi-file program (taken from the book Programming: a Modern approach, second edition By K. N. King) with a Makefile, but the compilation fails and I get the following warnings and don't understand why:
clang: warning: readline.o: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: inventory2.o: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-Wall' [-Wunused-command-line-argument]
here is the makefile:
############
# makefile #
############
CC = gcc
CFLAGS = -c -Wall
BIN=inventory2
$(BIN): $(BIN).o readline.o #readline.h
$(CC) $(CFLAGS) readline.o $(BIN).o -o $(BIN)
$(BIN).o: $(BIN).c
$(CC) $(CFLAGS) $(BIN).c -o $(BIN).o
readline.o: readline.c readline.h
$(CC) $(CFLAGS) readline.c -o readline.o
clean:
$(RM) -rf $(BIN) *.dSYM
$(RM) -rf readline.o
$(RM) -rf $(BIN).o
run: all
./$(BIN)
Here are the source files (inventory2.c, readline.c and readline.h)
/*********************************************************
* From C PROGRAMMING: A MODERN APPROACH, Second Edition *
* By K. N. King *
* Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
* All rights reserved. *
* This program may be freely distributed for class use, *
* provided that this copyright notice is retained. *
*********************************************************/
/* inventory.c (Chapter 16, page 391) */
/* Maintains a parts database (array version) */
#include <stdio.h>
#include "readline.h"
#define NAME_LEN 25
#define MAX_PARTS 100
struct part {
int number;
char name[NAME_LEN+1];
int on_hand;
} inventory[MAX_PARTS];
int num_parts = 0; /* number of parts currently stored */
int find_part(int number);
void insert(void);
void search(void);
void update(void);
void print(void);
/**********************************************************
* main: Prompts the user to enter an operation code, *
* then calls a function to perform the requested *
* action. Repeats until the user enters the *
* command 'q'. Prints an error message if the user *
* enters an illegal code. *
**********************************************************/
int main(void)
{
char code;
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != '\n') /* skips to end of line */
;
switch (code) {
case 'i': insert();
break;
case 's': search();
break;
case 'u': update();
break;
case 'p': print();
break;
case 'q': return 0;
default: printf("Illegal code\n");
}
printf("\n");
}
}
/**********************************************************
* find_part: Looks up a part number in the inventory *
* array. Returns the array index if the part *
* number is found; otherwise, returns -1. *
**********************************************************/
int find_part(int number)
{
int i;
for (i = 0; i < num_parts; i++)
if (inventory[i].number == number)
return i;
return -1;
}
/**********************************************************
* insert: Prompts the user for information about a new *
* part and then inserts the part into the *
* database. Prints an error message and returns *
* prematurely if the part already exists or the *
* database is full. *
**********************************************************/
void insert(void)
{
int part_number;
if (num_parts == MAX_PARTS) {
printf("Database is full; can't add more parts.\n");
return;
}
printf("Enter part number: ");
scanf("%d", &part_number);
if (find_part(part_number) >= 0) {
printf("Part already exists.\n");
return;
}
inventory[num_parts].number = part_number;
printf("Enter part name: ");
read_line(inventory[num_parts].name, NAME_LEN);
printf("Enter quantity on hand: ");
scanf("%d", &inventory[num_parts].on_hand);
num_parts++;
}
/**********************************************************
* search: Prompts the user to enter a part number, then *
* looks up the part in the database. If the part *
* exists, prints the name and quantity on hand; *
* if not, prints an error message. *
**********************************************************/
void search(void)
{
int i, number;
printf("Enter part number: ");
scanf("%d", &number);
i = find_part(number);
if (i >= 0) {
printf("Part name: %s\n", inventory[i].name);
printf("Quantity on hand: %d\n", inventory[i].on_hand);
} else
printf("Part not found.\n");
}
/**********************************************************
* update: Prompts the user to enter a part number. *
* Prints an error message if the part doesn't *
* exist; otherwise, prompts the user to enter *
* change in quantity on hand and updates the *
* database. *
**********************************************************/
void update(void)
{
int i, number, change;
printf("Enter part number: ");
scanf("%d", &number);
i = find_part(number);
if (i >= 0) {
printf("Enter change in quantity on hand: ");
scanf("%d", &change);
inventory[i].on_hand += change;
} else
printf("Part not found.\n");
}
/**********************************************************
* print: Prints a listing of all parts in the database, *
* showing the part number, part name, and *
* quantity on hand. Parts are printed in the *
* order in which they were entered into the *
* database. *
**********************************************************/
void print(void)
{
int i;
printf("Part Number Part Name "
"Quantity on Hand\n");
for (i = 0; i < num_parts; i++)
printf("%7d %-25s%11d\n", inventory[i].number,
inventory[i].name, inventory[i].on_hand);
}
File readline.c:
/*********************************************************
* From C PROGRAMMING: A MODERN APPROACH, Second Edition *
* By K. N. King *
* Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
* All rights reserved. *
* This program may be freely distributed for class use, *
* provided that this copyright notice is retained. *
*********************************************************/
/* readline.c (Chapter 16, page 395) */
#include <ctype.h>
#include <stdio.h>
#include "readline.h"
int read_line(char str[], int n)
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
while (ch != '\n' && ch != EOF) {
if (i < n)
str[i++] = ch;
ch = getchar();
}
str[i] = '\0';
return i;
}
and readline.h:
/*********************************************************
* From C PROGRAMMING: A MODERN APPROACH, Second Edition *
* By K. N. King *
* Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
* All rights reserved. *
* This program may be freely distributed for class use, *
* provided that this copyright notice is retained. *
*********************************************************/
/* readline.h (Chapter 16, page 395) */
#ifndef READLINE_H
#define READLINE_H
/**********************************************************
* read_line: Skips leading white-space characters, then *
* reads the remainder of the input line and *
* stores it in str. Truncates the line if its *
* length exceeds n. Returns the number of *
* characters stored. *
**********************************************************/
int read_line(char str[], int n);
#endif
You can simplify your Makefile by using implicit rules:
CC = gcc
CFLAGS = -Wall
BIN = inventory2
.PHONY: run clean
$(BIN): $(BIN).o readline.o
readline.o: readline.h
clean:
$(RM) $(BIN) *.o
run: $(BIN)
-./$(BIN)
Look at the Make output:
cc -c -Wall inventory2.c -o inventory2.o
cc -c -Wall readline.c -o readline.o
cc -c -Wall readline.o inventory2.o -o inventory2
cc: warning: readline.o: linker input file unused because linking not done
cc: warning: inventory2.o: linker input file unused because linking not done
The command you are using to attempt to link the executable includes a -c. That's because you have -c in the flags.
Also the run recipe depends on a non-existing target.
Remove the -o from the compilationg commands and the -c from the linking commands.
############
# makefile #
############
CC=cc
CFLAGS=-Wall
BIN=inventory2
$(BIN): $(BIN).o readline.o #readline.h
$(CC) $(CFLAGS) readline.o $(BIN).o -o $(BIN)
$(BIN).o: $(BIN).c
$(CC) $(CFLAGS) $(BIN).c -c
readline.o: readline.c readline.h
$(CC) $(CFLAGS) readline.c -c
clean:
$(RM) -rf $(BIN) *.dSYM
$(RM) -rf readline.o
$(RM) -rf $(BIN).o
run: $(BIN)
./$(BIN)
Related
Here is the callback with -v and warnings suppressed.
(base) goobnoob:~/c_projects/parts_database>gcc -w -v -o inventory inventory.c
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
"/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.10.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name inventory.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 253.9 -v -dwarf-column-info -resource-dir /Library/Developer/CommandLineTools/usr/bin/../lib/clang/7.0.2 -w -fdebug-compilation-dir /Users/goobnoob/c_projects/parts_database -ferror-limit 19 -fmessage-length 278 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.10.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/9d/665ynkj530z1n3999g4lzyvr0000gn/T/inventory-9d3105.o -x c inventory.c
clang -cc1 version 7.0.2 based upon LLVM 3.7.0svn default target x86_64-apple-darwin14.5.0
ignoring nonexistent directory "/usr/local/include"
#include "..." search starts here:
#include <...> search starts here:
/Library/Developer/CommandLineTools/usr/bin/../lib/clang/7.0.2/include
/Library/Developer/CommandLineTools/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0 -w -o inventory /var/folders/9d/665ynkj530z1n3999g4lzyvr0000gn/T/inventory-9d3105.o -lSystem /Library/Developer/CommandLineTools/usr/bin/../lib/clang/7.0.2/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"_read_line", referenced from:
_insert in inventory-9d3105.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here it is without warnings suppressed. But I don't believe that they are causing the issue.
(base) goobnoob:~/c_projects/parts_database>gcc -o inventory inventory.c
inventory.c:93:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
inventory.c:178:14: warning: data argument not used by format string [-Wformat-extra-args]
scanf("&d", &number);
~~~~ ^
inventory.c:212:75: warning: format specifies type 'long long' but the argument has type 'int' [-Wformat]
printf("%7d %-25s%lld\n", inventory[i].number, inventory[i].name, inventory[i].on_hand);
~~~~ ^~~~~~~~~~~~~~~~~~~~
%d
3 warnings generated.
Undefined symbols for architecture x86_64:
"_read_line", referenced from:
_insert in inventory-82371b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here is the code. It is a program for a very simple data base from a text book.
I truly believe my problem is with the compiling. It's been a while since I've used gcc and can't remember what I used. I won't be able to see the compile commands as I'm out right now.
I also have the supporting readline.h and readline.c files. If you need those just ask.
I'm also using running this on UNIX in OS X Yosemite.
/* Maintains a parts database (array version) */
#include <stdio.h>
#include "readline.h"
#define NAME_LEN 25
#define MAX_PARTS 100
int main();
int find_part(int number);
void insert();
void search();
void update();
void print();
struct part {
int number;
char name[NAME_LEN + 1]; /* accounting for space needed for \n break */
int on_hand;
} inventory[MAX_PARTS];
int num_parts = 0; /* number of parts currently stored */
int find_part (int number);
void insert(void);
void search(void);
void update(void);
void print(void);
/******************************************************************
* main : Prompts the user to enter an operation code, then calls *
* a function to perform the requested action. Repeats until*
* the user enters the command 'q'. Prints an error message *
* if the user enters an illegal code. *
******************************************************************/
int main(void){
char code;
for (;;){
printf("Enter operation code: ");
scanf(" %c", &code);
while(getchar() != '\n'); /* skips to end of line */
switch (code) {
case 'i' : insert();
break;
case 's' : search();
break;
case 'u' : update();
break;
case 'p' : print();
break;
case 'q' : return 0;
default : printf("Illegal Code!\n");
}
printf("\n");
}
}
/****************************************************************
* find_part : Looks up a part number in the inventory array. *
* Returns the array index if the part number is *
* found; otherwise, returns -1. *
****************************************************************/
int find_part (int number){
int i;
for (i = 0; i < num_parts; i++){
if (inventory[i].number == number){
return i;
}
return -1;
}
}
/*****************************************************************
* insert : Prompts the user for information about a new part and *
* then inserts the part into the database. Prints an *
* error message and returns prematurely if the part *
* already exists or the database is full. *
*****************************************************************/
void insert(void){
int part_number;
if (num_parts == MAX_PARTS) {
printf("Database is full; can't add more parts.\n");
return;
}
printf("Enter part number: ");
scanf("%d", &part_number);
if (find_part(part_number) >= 0){
printf("Part already exists");
return;
}
/*Now we input the new part */
inventory[num_parts].number = part_number;
printf("Enter Part Name : ");
read_line(inventory[num_parts].name, NAME_LEN); /* Getting Part Name of no longer length than NAME_LEN */
printf("Enter quantity on hand : ");
scanf("%d", &inventory[num_parts].on_hand);
num_parts++; /* Updating amount of Parts in Storage */
}
/******************************************************************
* search : Prompts the user to enter a part number, then looks up *
* the part in the database. If the part exists, prints *
* the name and quantity on hand; if not, prints an error *
* message. *
******************************************************************/
void search(void){
int i, number;
printf("Enter part number : ");
scanf("%d", &number);
i = find_part(number);
if (i >= 0){ /* Verifying the part number exists (aka. did not return -1) */
printf("Part name : %s\n", inventory[i].name);
printf("Quantity on hand : %d\n", inventory[i].on_hand);
}else{
printf("Part not found!\n");
}
}
/****************************************************************
* update : Prompts the user to enter a part number. Prints an *
* error message if the part doesn't exist; otherwise, *
* prompts the user to enter change in quantity on hand *
* and updates the data base. *
****************************************************************/
void update(void){
int i, number, change;
printf("Enter Part Number : ");
scanf("&d", &number);
i = find_part(number);
if (i >= 0){
printf("Enter change in quantity on hand : ");
scanf("%d", &change);
inventory[i].on_hand += change;
}else{
printf("Part not found!\n");
}
}
/*****************************************************************
* print : Prints a listing of all parts in the database, showing *
* the part number, part name, and quantity on hand. *
* Parts are printed in the order in which they were *
* entered into the database. *
*****************************************************************/
void print(void){
int i;
printf("Part Number Part Name Quantity on Hand");
for (i = 0; i < num_parts; i++){
printf("%7d %-25s%lld\n", inventory[i].number, inventory[i].name, inventory[i].on_hand);
}
}
I also have the supporting readline.h and readline.c files. If you need those just ask.
You do not appear to be compiling readline.c at all. Try:
gcc -w -v -o inventory inventory.c readline.c
I need to compile a Lua 5.2.4 module with gcc 5.3.0. on Windows.
Before doing that, I build the Lua 5.2.4 in the following steps:
gcc -c -DLUA_BUILD_AS_DLL *.c
ren lua.o lua.obj
ren luac.o luac.obj
ar rcs lua524-static.lib *.o
gcc -shared -o lua524.dll -Wl,--out-implib,lua524.lib *.o
gcc lua.obj lua524.lib -o lua524.exe
gcc luac.obj lua524-static.lib -o luac524.exe
del *.o *.obj
The dynamic library (.dll) I want to create is written in the following (let's call it LuaMath:
#include<windows.h>
#include<math.h>
#include "lauxlib.h"
#include "lua.h"
static int IdentityMatrix(lua_State *L)
{
int in = lua_gettop(L);
if (in!=1)
{
lua_pushstring(L,"Maximum 1 argument");
lua_error(L);
}
lua_Number n = lua_tonumber(L,1);
lua_newtable(L); /* tabOUT n */
int i,j;
for (i=1;i<=n;i++)
{
lua_newtable(L); /* row(i) tabOUT n */
lua_pushnumber(L,i); /* i row(i) tabOUT n */
for (j=1;j<=n;j++)
{
lua_pushnumber(L,j); /* j i row(i) tabOUT n */
if (j==i)
{
lua_pushnumber(L,1);
}
else /* 0/1 j i row(i) tabOUT n */
{
lua_pushnumber(L,0);
}
/* Put 0/1 inside
row(i) at j
position */
lua_settable(L,-4); /* i row(i) tabOUT n */
}
lua_insert(L,-2); /* row(i) i tabOUT n */
/* Insert row(i) into
position in tabOUT */
lua_settable(L,2); /* tabOUT n */
}
return 1;
}
static const struct luaL_Reg LuaMath [] = {{"IdentityMatrix", IdentityMatrix},
{ NULL, NULL}};
LUA_API int luaopen_LuaMath(lua_State *L)
{
luaL_newlib(L,LuaMath);
return 1;
}
As stated here, I build the above code as follow:
gcc -O2 -c -DLUA_BUILD_AS_DLL -o LuaMath.o LuaMath.c
gcc -O -shared -o LuaMath.dll LuaMath.o -L. -llua524
when I run the following Lua code:
require("LuaMath")
A=LuaMath.IdentityMatrix(2)
the output error is:
stdin:1: attempt to index global 'LuaMath' (a nil value)
stack traceback:
stdin:1: in main chunk
[C]: in ?
What do I do wrong?
Thanks in advance
Your C code is correct. The usual Lua idiom is
LuaMath=require("LuaMath")
if you want to load your library into a global variable.
If you want a local variable, use
local LuaMath=require("LuaMath")
The following RPC program executes very slowly on Fedora.
If I change the size of the name buffer from 999 characters to 512 in llist.x, then it's working fast.
I don't know why.
If anyone knows the reason, please let me know!
Note: Please compile the following programs and execute the server then the client.
(For me, it takes 3 seconds for 30 loops.)
llist.c
#include "llist.h"
#define PRINT_TIME (!gettimeofday(&tv, NULL) && printf(" %lf",tv.tv_sec+(tv.tv_usec/1000000.0)))
struct timeval tv;
int main(int argc, char *argv[])
{
CLIENT *cl;
int *result,i=0;
cl = clnt_create("localhost", PRINTER, PRINTER_V1, "tcp");
if (cl == NULL) {
clnt_pcreateerror("Cant Create Client Handle");
printf("error: could not connect to server.\n");
return 1;
}
ST1 key[1];
ST1_stuff key_x;
/*key_x.ST1_stuff_val = key;
key_x.ST1_stuff_len = 1;
*/
while(i<30)
{
printf("\n %d -> start - ",i);
PRINT_TIME;
result = sei_conf_test_keys2_1(&key_x,cl);
if (result == NULL) {
printf("error: RPC failed!\n");
return 1;
}
printf("\nresult = %d ",*result);
i++;
printf("\n end - ");
PRINT_TIME;
printf("\n -------------------------------------");
}
return 0;
}
llist_svc_proc.c
#include <stdlib.h>
#include "llist.h"
int result;
int *sei_conf_test_keys2_1_svc(ST1 *lst, struct svc_req *req)
{
result = 0;
return &result;
}
llist.x
struct s1{
char name[999]; /* <===== HERE */
};
typedef struct s1 ST1;
typedef ST1 ST1_stuff[1];
program PRINTER {
version PRINTER_V1 {
int SEI_CONF_TEST_KEYS2(ST1_stuff) = 10;
} = 1;
} = 0x2fffffff;
makefile
.SUFFIXES:
.SUFFIXES: .c .o
CLNT = llist
SRVR = llist_svc
CFLAGS = -g -Wall
SRVR_OBJ = llist_svc_proc.o llist_xdr.o llist_svc.o
CLNT_OBJ = llist.o llist_xdr.o llist_clnt.o
.c.o:
gcc -c -o $# $(CFLAGS) $<
default: $(CLNT) $(SRVR)
$(CLNT): $(CLNT_OBJ) llist.h
gcc -o $(CLNT) $(CLNT_OBJ)
$(SRVR): $(SRVR_OBJ) llist.h
gcc -o $(SRVR) $(SRVR_OBJ)
clean:
rm *.o $(CLNT) $(SRVR)
rm -f llist_xdr.c llist.h llist_clnt.c llist_svc.c
rm core
The time increase seems to be connected with the maximum allowed
data in one TCP package.
Using a network analyser, one sees, that with size 999 there are 167 packages
whereas with size 512 only about 79 packets are sent between client and server.
The maximum data size per package seems to be 4000 bytes.
If you need performance, think about switching to UDP which seems not be limited by the maximum package size and additional overhead.
I was doing some test coding for generating the pattern base on given pattern string and done as follows:
The header file is test.h:
#ifndef test_h
#define test_h
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
extern char uid []= "123456789561";
void generate_uid(FILE *,char *, char);
#endif
The .c file are as follows: test.c
#include"test.h"
extern int count;
void generate_uid(FILE *fp,char *uid_t, char ch)
{
int index = rand()%12;
int i,j;
strcpy(uid_t,uid);
if(ch == ' ')
{
for(j=3;j<10;j+=6)
{
uid_t[j] = ch;
}
}
// Replace numbers with special chars or alphabets(small/capital)
else
{
if(index < 6)
{
for(i=0;i<index;i++)
{
uid_t[i]=ch;
}
}
else
{
for(i=index;i<strlen(uid);i++)
{
uid_t[i]=ch;
}
}
}
count++;
fprintf(fp,"\"%s\", ",uid_t);
return;
}
main.c:
#include"test.h"
int count = 0;
int main()
{
FILE *fp_char,*fp_test;
char invalid_chars;
char *uid_t = (char *)malloc(sizeof(char)*14);
fp_char = fopen("invalidchars.txt","r");
fp_test = fopen("uid_test.txt","w");
if(fp_test == NULL || fp_char == NULL)
{
printf("cannot open file.\n");
return;
}
while((invalid_chars = fgetc(fp_char)) != EOF)
{
if(invalid_chars == '\n')
{
continue;
}
generate_uid(fp_test,uid_t, invalid_chars);
}
//Greater than 12 digit
strcpy(uid_t,uid);
strcat(uid_t,"2");
count++;
fprintf(fp_test,"\"%s\", ",uid_t);
//Less than 12 digit
strcpy(uid_t,uid);
uid_t[11]='\0';
count++;
fprintf(fp_test,"\"%s\", ",uid_t);
count++;
fprintf(fp_test,"\"%s\", ","NULL");
count++;
fprintf(fp_test,"\"%s\", ","empty");
free(uid_t);
fclose(fp_char);
fclose(fp_test);
printf("Number of string count : %d\n",count);
return 0;
}
Makefile is:
all : test.o main.o run
run : test.o main.o
$(CC) -g $^ -o $#
%.o : %.c
${CC} -g -c $< -o $#
.PHONY : clean
clean :
-rm -f *.o run
When I was compiling it gives the following:
cc -g -c test.c -o test.o
cc -g -c main.c -o main.o
cc -g test.o main.o -o run
main.o:(.data+0x0): multiple definition of `uid'
test.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
make: *** [run] Error 1
Where I am going wrong.
You can declare a global variable in a header file, but not attribute it a value, because you can do it only once. So if you include your test.h file in more than one .c file (that would be usual), your compiler will see many initializations of the same thing when it will assemble your .o files.
Let only that in your test.h file :
extern char uid [];
And in one specific .c file (like uid.c for instance), initalize it :
char uid []= "123456789561";
Then add this new file to your Makefile.
You defined in header object
extern char uid []= "123456789561";
and this definition now is present in each compilation unit where the header is included. So the same object is defined several times and the compiler issues an error.
You should declare it in the header as for example
extern char uid [13];
and define it in one of the modules
char uid []= "123456789561";
I am trying to compile a simple introductory program using ffmpeg that tries to check if the mp3 codec is available. While the code compiles OK, I am facing difficulty in solving linker errors. Here is the code:
#include <stdio.h>
#include <math.h>
#include <libavcodec/avcodec.h>
float *generateSinusoid(unsigned int sampleRate, unsigned int nSecondsAudio) {
unsigned int nsamples = (nSecondsAudio * sampleRate);
float *arr;
arr = (float*) malloc(sizeof(float) * nsamples);
int i = 0;
for(i = 0; i < nsamples; i++) {
arr[i] = 20 * sin(2.f * (M_PI) * (330/sampleRate) * i); /*frequency of 330H
z*/
}
return arr;
}
int main(int argc, char *argv[]) {
avcodec_register_all();
AVCodec *codec;
unsigned int sampleRate = 22050; /*assumed.*/
unsigned int nSecondsAudio = 4;
float *arr;
arr = (float *) malloc(sizeof(float) * nSecondsAudio * sampleRate);
/*Step 1. Generate sinusoid.*/
arr = generateSinusoid(sampleRate, nSecondsAudio);
/* Step 2. See if encoder exists.*/
/*codec = avcodec_find_encoder(AV_CODEC_ID_MP3);*/
if(!codec) { /*codec = NULL.*/
printf("MP3 codec not found!!!!");
} else {
printf("MP3 codec found!!!");
}
return 0;
}
The code is compiled and linked like so:
encoding_mp3: encoding_mp3.o
gcc encoding_mp3.o -o encoding_mp3 -L/cygdrive/c/Users/Desktop/webserver/cygnus/lib/w32api -L/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/lib -lm -luser32 -lpthread -lavcodec
encoding_mp3.o: encoding_mp3.c
gcc -I/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/include -I/cygdrive/c/Users/Desktop/webserver/cygnus/usr/include -g -c encoding_mp3.c -o encoding_mp3.o
clean:
rm encoding_mp3.o encoding_mp3
Linking gives the following error:
gcc -I/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/include -I/cygdrive/c/Users/Desktop/webserver/cygnus/usr/include -g -c encoding_mp3.c -o encoding_mp3.o
gcc encoding_mp3.o -o encoding_mp3 -L/cygdrive/c/Users/Desktop/webserver/cygnus/lib/w32api -L/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/lib -lm -luser32 -lpthread -lavcodec
encoding_mp3.o: In function `main':
/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/work/encoding_mp3.c:31: undefined reference to `_avcodec_register_all'
collect2: ld returned 1 exit status
make: *** [encoding_mp3] Error 1
I have gone through most of the threads on SO regarding this problem and here is what I have tried so far:
- Put libraries at the end of all non-option arguments
- Commented out code that references functions. This seems to work. The undefined reference errors go away after all function calls are removed, though the presence of a struct AVCodec does not cause any problems.
Any help on this is most welcome.