I am trying to build a web crawler and need to parse URIs.
I have tried to compile some simple uriparser code which I copied and pasted from https://uriparser.github.io/doc/api/latest/
#include <uriparser/Uri.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
UriUriA uri;
const char * const uriString = "file:///home/user/song.mp3";
const char * errorPos;
if (uriParseSingleUriA(&uri, uriString, &errorPos) != URI_SUCCESS) {
/* Failure (no need to call uriFreeUriMembersA) */
return 1;
}
/* Success */
uriFreeUriMembersA(&uri);
}
The code doesn't compile and gives the following errors:
/tmp/ccWkKj10.o: In function `main':
uri.c:(.text+0x51): undefined reference to `uriParseSingleUriA'
uri.c:(.text+0x6b): undefined reference to `uriFreeUriMembersA'
collect2: error: ld returned 1 exit status
I have the latest version of liburiparser-dev installed (0.9.3-2).
Any idea what I am doing wrong?
EDIT
Adding flag -luriparser solved the problem.
Related
i have a c file named main.c
#include <stdio.h>
extern int main(int argc, char* argv[], char* envp[]);
void start(){
...;
int return_code = main(argc, argv, envp);
exit(return_code);
}
you can see I declared main but when using ld to link it:
$ (use ld to link, I didn't write it down because it's quite verbose and irrelevant)
ld: bin/test.o: in function `start':
/home/user/Desktop/test/test.c:28: undefined reference to `main'
make: *** [Makefile:49: link] Error 1
so what should i do (sorry if this is a simple question for you)
In C you have to define a main function that will be called automatically by your program, this is the base of your code.
I saw that you include "stdio.h" which is a library allowing to have access to some function like for example in my program the function "printf".
If you don't need it then don't include it :)
For example here is how to make your first program with a main function.
#include <stdio.h>
int main(int argc, char *argv[]){
... // Your code
printf("Hello world"); // Just print on your terminal this string
return (0); // 0 is the default return code if there is no errors
}
Generally speaking, invoking ld yourself is being a glutton for punishment. Use your C compiler to link until proven otherwise.
gcc -o bin/test bin/test.o will link a C program for you.
It looks like you tried to "fix" it by providing _start yourself. You can't (in C). _start is not a function.
I am learning to write apps on ESP32 platform. When I was trying to compile my code, I got this error from linker:
undefined reference to `Serial_Init'
Where Serial_Init is a function declared in a file serial_cli.h which is in the same directory and workspace. What's more, I declared some macros there, and can use them no problem in my code, so I really don't understand where the error comes from.
Here's serial_cli.h:
#ifndef _SERIAL__H_
#define _SERIAL__H_
#include "driver/uart.h"
#define SERIAL_PORT UART_NUM_1
#define RTS_SIG_PINOUT UART_PIN_NO_CHANGE
#define CTS_SIG_PINOUT UART_PIN_NO_CHANGE
/**
* #brief This function initialises serial communication with PC
* #param uart_config_t type pointer to structure containing needed parameters
* #param int size of RX and TX buffer (in bytes)
* #param QueueHandle_t type pointer to structure containing UART queue
*/
void Serial_Init(uart_config_t*, const int, QueueHandle_t*);
#endif /* _SERIAL_H_ */
And here's serial_cli.c:
#include "serial_cli.h"
void Serial_Init(uart_config_t* uart_config, const int buffer_size, QueueHandle_t* queue)
{
ESP_ERROR_CHECK(uart_param_config(SERIAL_PORT, uart_config));
ESP_ERROR_CHECK(uart_set_pin(SERIAL_PORT, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, RTS_SIG_PINOUT, CTS_SIG_PINOUT));
ESP_ERROR_CHECK(uart_driver_install(SERIAL_PORT, buffer_size, buffer_size, 10, queue, 0));
}
And finally, main body of the app:
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "serial_cli.h"
#include "string.h"
void app_main(void)
{
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
.rx_flow_ctrl_thresh = 122
};
QueueHandle_t queue;
Serial_Init(&uart_config, 1024, &queue);
char* test_str = "This is a test string.\n";
while(1) {
uart_write_bytes(SERIAL_PORT, (const char*)test_str, strlen(test_str));
vTaskDelay(500);
}
}
Below I also include complete output that I get from console:
D:\Tools\ESP_IDF\examples\get-started\blink>idf.py build
Executing action: all (aliases: build)
Running ninja in directory d:\tools\esp_idf\examples\get-started\blink\build
Executing "ninja all"...
[1/8] Performing build step for 'bootloader'
ninja: no work to do.
[5/6] Linking CXX executable blink.elf
FAILED: blink.elf
cmd.exe /C "cd . && D:\Tools\ESP_IDF_container\.espressif\tools\xtensa-esp32-elf\esp-2020r3-8.4.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g++.exe -mlongcalls -Wno-frame-address #CMakeFiles\blink.elf.rsp -o blink.elf
&& cd ."
d:/tools/esp_idf_container/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/main/libmain.a(blink.c.obj):(.literal.app_main+0x8): undefined reference to `Serial_Init'
d:/tools/esp_idf_container/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/main/libmain.a(blink.c.obj): in function `app_main':
d:\tools\esp_idf\examples\get-started\blink\build/../main/blink.c:38: undefined reference to `Serial_Init'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
ninja failed with exit code 1
Thanks in advance for any feedback.
You need to add "serial_cli.c" to your main/CMakeLists.txt. Something like this:
idf_component_register(
SRCS
"blink.c"
"serial_cli.c"
...
See details in ESP IDF documentation
I'm trying out the examples in the xmlrpc-c documentation:
#include <stdio.h>
#include <xmlrpc.h>
#include <xmlrpc_server.h>
//#include <xmlrpc_server_abyss.h>
#include <xmlrpc_abyss.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/util.h>
static xmlrpc_value *
sample_add(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const serverContext) {
xmlrpc_int32 x, y, z;
/* Parse our argument array. */
xmlrpc_decompose_value(envP, paramArrayP, "(ii)", &x, &y);
if (envP->fault_occurred)
return NULL;
/* Add our two numbers. */
z = x + y;
/* Return our result. */
return xmlrpc_build_value(envP, "i", z);
}
int
main (int const argc,
const char ** const argv) {
xmlrpc_server_abyss_parms serverparm;
xmlrpc_registry * registryP;
xmlrpc_env env;
xmlrpc_env_init(&env);
registryP = xmlrpc_registry_new(&env);
xmlrpc_registry_add_method(
&env, registryP, NULL, "sample.add", &sample_add, NULL);
serverparm.config_file_name = argv[1];
serverparm.registryP = registryP;
printf("Starting XML-RPC server...\n");
xmlrpc_server_abyss(&env, &serverparm, XMLRPC_APSIZE(registryP));
return 0;
}
I try to compile using gcc:
gcc source.c
nohting fancy and I get:
/tmp/ccfGuc6A.o: In function sample_add':
source.c:(.text+0x38): undefined reference toxmlrpc_decompose_value'
source.c:(.text+0x6d): undefined reference to xmlrpc_build_value'
/tmp/ccfGuc6A.o: In functionmain':
source.c:(.text+0x96): undefined reference to xmlrpc_env_init'
source.c:(.text+0xa5): undefined reference toxmlrpc_registry_new'
source.c:(.text+0xd8): undefined reference to xmlrpc_registry_add_method'
source.c:(.text+0x117): undefined reference toxmlrpc_server_abyss'
collect2: error: ld returned 1 exit status
these functions exist in the:
/usr/include/xmlrpc-c/base.h
whihc I have referenced:
include
I think I'm not passing the right options to link, I don't know how it's done though.
thanks
You definitely don't pass the correct argument for the linker. Just including a header file doesn't actually make the linker link with the library, you need to use the -l (lower-case L) option to tell the linker which libraries you need to link with, like
gcc source.c -lxmlrpc
I believe that xml-rpc-c comes with a helper program, intended to help you get the linking right. Its documented here
http://xmlrpc-c.sourceforge.net/doc/xmlrpc-c-config.html
This question already has answers here:
Cuda C - Linker error - undefined reference
(2 answers)
Closed 7 years ago.
I'm new to CUDA programming hence running into issues with compiling/ linking files. I'm trying to compile .c and .cu files.
Here are the files:
p3.c:
#include <stdlib.h>
#include <stdio.h>
extern void load_scheduler(int k, int j);
int blocks, threads;
int main(int argc, char* argv[])
{
if (argc > 1)
{
blocks = atoi(argv[1]);
threads = atoi(argv[2]);
}
else
exit(1);
load_scheduler(blocks, threads);
}
And scheduler.cu file:
#include <stdlib.h>
#include <stdio.h>
__global__ void sched_func()
{
int j = 6*5*threadIdx.x;
printf("%d\n",j);
}
void load_scheduler(int b, int n)
{
sched_func<<< b,n >>>();
}
I compile these two files using nvcc -c scheduler.cu p3.c and it seems fine
However, when I try to link these two files using nvcc -o cuda_proj scheduler.o p3.o, I get an error:
p3.o: In function `main':
p3.c:(.text+0x58): undefined reference to `load_scheduler'
collect2: ld returned 1 exit status
I may not be using the right steps to get this working, so if there's any other way I should try out, suggestions are welcome. I am also new to making Makefiles so want to stick to using nvcc commands on terminal.
Just added : extern "c" before load_scheduler definition. NVCC could not recognize the function definition as it belonged to .cu file, therefore the error.
extern "C"
void load_scheduler(int b, int n)
{
sched_func<<< b,n >>>();
}
Here is some code from Ben Straub's (link blog) that I am basing this on:
static int do_clone(const char *url, const char *path)
{
git_repository *repo = NULL;
int ret = git_clone(&repo, url, path, NULL);
git_repository_free(repo);
return ret;
}
And here is my code:
#include <git2.h>
int main(void) {
git_repository *out = NULL;
git_clone(&out, "https://github.com/lehitoskin/racketball", "/home/maxwell", NULL);
return 0;
}
I am very inexperienced with C, so I apologize for having such elementary problems. Anyway, here is the error my compiler gives me:
maxwell#max-pc ~ $ gcc -I libgit2/include gitfun.c
/tmp/ccB64nPh.o: In function `main':
gitfun.c:(.text+0x31): undefined reference to `git_clone'
collect2: error: ld returned 1 exit status
Why can't I call git_clone this way?
It looks like you didn't link to the library. Add -lgit2 if libgit2 is the lib name.
gcc -I libgit2/include gitfun.c -L<path to lib> -l<libname minus the "lib" part>
IOW, you compile fine but when the linker goes looking for git_clone it can't find it because you haven't specified the library that it is in.