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
Related
I am building a DLL to embed Python 2.7 (2.7.18 to be specific) in a larger application largely following python.org's C API documentation. When I believe I am applying Py_DECREF correctly to a PyString PyObject, any linking application crashes without error. The code is largely copied from a similar application built with python 3.8, 3.9 and 3.10 that in fact works.
// python_functions.h
__declspec(dllexport) int init_python();
// python_functions.c
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdio.h> // printf
#include <string.h>
//#include <wchar.h> // wchar_t for python3 only
#include "python_functions.h"
__declspec(dllexport) int init_python() {
int rv = -1;
printf("attempting to initialize Python interpreter...\n");
// I have other python versions, Python27 not on path. set PYTHONHOME
Py_SetPythonHome("C:\\Python27\\"); // python27
//Py_SetPythonHome(L"C:\\Python38\\"); // python3X
Py_Initialize();
PyObject * pModule_Name = PyString_FromString("python_functions"); // python27 only
//PyObject * pModule_Name = PyUnicode_FromString("python_functions"); // python3X
if (!pModule_Name) {
printf("failed to convert module name to python string object\n");
return rv;
}
// can confirm pModule_Name != NULL && Py_REFCNT(pModule_Name) == 1 here
/*
load python module named "python_functions"
*/
// can also confirm pModule_Name != NULL && Py_REFCNT(pModule_Name) == 1 here
//Py_DECREF(pModule_Name); // If I uncomment this line, application linking .dll crashes
printf("pModule_Name successfully used\n");
rv = 0;
return rv;
}
// main.c
#include <stdio.h>
#include "python_functions.h"
int main(int argc, char **argv) {
int rv = 0;
// crashes without error if Py_DECREF(pModule_Name) in python_functions.c uncommented
printf("%d\n", init_python());
return rv;
}
Compiling the .dll with gcc 8.1.0 (MinGW-64) linking against C:\Python27\python27.dll:
gcc -IC:\Python27\include -LC:\Python27\libs -IC:\Python27 python_functions.c -shared -o python_functions.dll -lpython27
Compiling main with python27.dll and python_functions.dll in the same directory:
gcc main.c -o main.exe -L./ -lpython_functions
If I uncomment the line Py_DECREF(pModule_Name), which I thought I must do since PyString_FromString returns a new reference, then running main.exe crashes without return.
Should I be using something else or not doing reference counting on result from PyString_FromString or is this a larger problem with linking to python27?
If I make the commented/corresponding compiler changes to link against python 3.8+, no problems...
The only differences I know of are the significant changes to the Python C API between 2.7 and e.g. 3.8+ as well as the differences in python 2.X vs 3.X string representations. I believe PyString is appropriate here because when I actually implement and load a module name python_functions.py, it works...so long as I leave Py_DECREF(pModule_Name) commented out and the pointer leaked.
I also thought PyString_FromString might have a problem passing a string literal, but it accepts a const char *, documentation says it creates a copy, and even passing a copy of the string literal to another variable does not work.
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.
So I'm a noob when it comes to this sort of stuff.
I'm struggling to compile a climate model on macOS and I've boiled it down to what is going on here:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
#include <sys/resource.h>
#include <sys/syscall.h>
static pid_t gettid(void)
{
return syscall(__NR_gettid);
}
/*
* Returns this thread's CPU affinity, if bound to a single core,
* or else -1.
*/
int get_cpu_affinity(void)
{
cpu_set_t coremask; /* core affinity mask */
CPU_ZERO(&coremask);
if (sched_getaffinity(gettid(),sizeof(cpu_set_t),&coremask) != 0) {
fprintf(stderr,"Unable to get thread %d affinity.
%s\n",gettid(),strerror(errno));
}
int cpu;
int first_cpu = -1; /* first CPU in range */
int last_cpu = -1; /* last CPU in range */
for (cpu=0;cpu < CPU_SETSIZE;cpu++) {
if (CPU_ISSET(cpu,&coremask)) {
if (first_cpu == -1) {
first_cpu = cpu;
} else {
last_cpu = cpu;
}
}
}
return (last_cpu == -1) ? first_cpu : -1;
}
int get_cpu_affinity_(void) { return get_cpu_affinity(); } /* Fortran interface */
/*
* Set CPU affinity to one core.
*/
void set_cpu_affinity( int cpu )
{
cpu_set_t coremask; /* core affinity mask */
CPU_ZERO(&coremask);
CPU_SET(cpu,&coremask);
if (sched_setaffinity(gettid(),sizeof(cpu_set_t),&coremask) != 0) {
fprintf(stderr,"Unable to set thread %d affinity. %s\n",gettid(),strerror(errno));
}
}
void set_cpu_affinity_(int *cpu) { set_cpu_affinity(*cpu); } /* Fortran interface */
When compiling I get a couple of errors:
First - identifier "cpu_set_t" is undefined, Second - identifier "CPU_SETSIZE" is undefined
I've done my googling and it seems to me that sched_getaffinitiy(), cpu_set_t and perhaps some other things are not present in macOS.
With all kinds of C programming I'm really out of my depth. I was wondering if anyone here knows of an alternative way of doing this for macOS and how I could go about doing that.
I've attached the full error report below.
Kind Regards,
Neil :)
Complete error report:
(python2) salvare:MASTERS Neil$ python run.py
Working directory for exp 'playground' already exists
RRTM compilation disabled. Namelist set to gray radiation.
Writing path_names to '/Users/Neil/MASTERS/ISCA_TEMP/playground/path_names'
Running compiler
/Users/Neil/MASTERS/ISCA_TEMP/playground/compile.sh: line 21: module: command not found
loadmodules
/Users/Neil/MASTERS/Isca/src/extra/loadmodule: line 3: module: command not found
/Users/Neil/MASTERS/Isca/src/extra/loadmodule: line 4: module: command not found
/Users/Neil/MASTERS/Isca/src/extra/loadmodule: line 5: module: command not found
/Users/Neil/MASTERS/Isca/src/extra/loadmodule: line 6: module: command not found
/Users/Neil/MASTERS/ISCA_TEMP/playground/compile.sh: line 23: module: command not found
/Users/Neil/MASTERS/ISCA_TEMP/playground/compile.sh: line 24: ulimit: stack size: cannot modify limit: Operation not permitted
./compile_mppn.sh: line 13: module: command not found
./compile_mppn.sh: line 14: module: command not found
./compile_mppn.sh: line 15: module: command not found
- mppnccombine.c:162:24: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
sprintf(outfilename,argv[outputarg]); outlen=strlen(outfilename);
^~~~~~~~~~~~~~~
/usr/include/secure/_stdio.h:47:56: note: expanded from macro 'sprintf'
__builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
^~~~~~~~~~~
mppnccombine.c:162:24: note: treat the string as an argument to avoid this
sprintf(outfilename,argv[outputarg]); outlen=strlen(outfilename);
^
"%s",
/usr/include/secure/_stdio.h:47:56: note: expanded from macro 'sprintf'
__builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
^
1 warning generated.
ln: /Users/Neil/MASTERS/ISCA_TEMP/playground/exec/mppnccombine.x: File exists
Makefile is ready.
mpicc -Duse_libMPI -Duse_netCDF -Duse_LARGEFILE -DINTERNAL_FILE_NML -DOVERLOAD_C8 -DRRTM_NO_COMPILE -I/usr/local/include -D__IFC -c /Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c
......................................................................................................................../Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(35): error: identifier "__NR_gettid" is undefined
eturn syscall(__NR_gettid);
^
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(44): error: identifier "cpu_set_t" is undefined
cpu_set_t coremask; /* core affinity mask */
^
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(47): error: identifier "cpu_set_t" is undefined
if (sched_getaffinity(gettid(),sizeof(cpu_set_t),&coremask) != 0) {
^
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(54): error: identifier "CPU_SETSIZE" is undefined
for (cpu=0;cpu < CPU_SETSIZE;cpu++) {
^
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(75): error: identifier "cpu_set_t" is undefined
cpu_set_t coremask; /* core affinity mask */
^
/Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c(79): error: identifier "cpu_set_t" is undefined
if (sched_setaffinity(gettid(),sizeof(cpu_set_t),&coremask) != 0) {
^
compilation aborted for /Users/Neil/MASTERS/Isca/src/shared/mpp/affinity.c (code 2)
make: *** [affinity.o] Error 2
ERROR: mkmf failed for fms_moist
CRITICAL - Compilation failed.
Traceback (most recent call last):
File "run.py", line 25, in <module>
exp.compile()
File "/Users/Neil/MASTERS/Isca/src/extra/python/gfdl/experiment.py", line 298, in compile
raise e
sh.ErrorReturnCode_1:
RAN: /bin/bash /Users/Neil/MASTERS/ISCA_TEMP/playground/compile.sh
STDOUT:
STDERR:
Your googling put you on the right track - sched_getaffinity() and the cpu_set_t structure that it uses are linux-specific, and are not available on all platforms. Mac OSX in particular seems to be missing them.
However, there are alternatives. I found a blog post from someone porting code to OSX who found an alternative using sysctlbyname(). The code on that site reimplements sched_getaffinity() on Mac OSX by requesting the machdep.cpu.core_count, and using that to build their own version of cpu_set_t.
Somewhere between my headers and my Makefile I'm not doing the dependencies correctly, and it's not compiling. This really only has anything to do with the first few lines from each code, but I posted all the code for reference
I'm trying to split up a who clone into 3 parts. Here is the original for reference. The exercise is to make it with utmp, so you also need utmplib
So I've split it up into 3 files, the first one being show.h
#include <stdio.h>
#include <sys/types.h>
#include <utmp.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#define SHOWHOST
void show_info(struct utmp *);
void showtime(time_t);
then I have show.c
/*
* * show info()
* * displays the contents of the utmp struct
* * in human readable form
* * * displays nothing if record has no user name
* */
void show_info( struct utmp *utbufp )
{
if ( utbufp->ut_type != USER_PROCESS )
return;
printf("%-8.8s", utbufp->ut_name); /* the logname */
printf(" "); /* a space */
printf("%-8.8s", utbufp->ut_line); /* the tty */
printf(" "); /* a space */
showtime( utbufp->ut_time ); /* display time */
#ifdef SHOWHOST
if ( utbufp->ut_host[0] != '\0' )
printf(" (%s)", utbufp->ut_host); /* the host */
#endif
printf("\n"); /* newline */
}
void showtime( time_t timeval )
/*
* * displays time in a format fit for human consumption
* * uses ctime to build a string then picks parts out of it
* * Note: %12.12s prints a string 12 chars wide and LIMITS
* * it to 12chars.
* */
{
char *ctime(); /* convert long to ascii */
char *cp; /* to hold address of time */
cp = ctime( &timeval ); /* convert time to string */
/* string looks like */
/* Mon Feb 4 00:46:40 EST 1991 */
/* 0123456789012345. */
printf("%12.12s", cp+4 ); /* pick 12 chars from pos 4 */
}
and finally, `who3.c'
/* who3.c - who with buffered reads
* - surpresses empty records
* - formats time nicely
* - buffers input (using utmplib)
*/
#include "show.h"
int main()
{
struct utmp *utbufp, /* holds pointer to next rec */
*utmp_next(); /* returns pointer to next */
if ( utmp_open( UTMP_FILE ) == -1 ){
perror(UTMP_FILE);
exit(1);
}
while ( ( utbufp = utmp_next() ) != ((struct utmp *) NULL) )
show_info( utbufp );
utmp_close( );
return 0;
}
So I created my Makefile:
who3:who3.o utmplib.o
gcc -o who who3.o utmplib.o
who3.o:who3.c show.c
gcc -c who3.c show.o
show.o:show.c
gcc -c show.c show.h
utmplib.o:utmplib.c
gcc -c utmplib.c
clean:
rm -f *.o
Unfortunately there's an error when I do make:
gcc -o who who3.o utmplib.o
who3.o: In function `main':
who3.c:(.text+0x38): undefined reference to `show_info'
collect2: error: ld returned 1 exit status
make: *** [who3] Error 1
As I said earlier, I haven't done my dependencies correctly, and I'm not sure what I did wrong. How do I do my dependencies correctly?
It looks like you are missing show.o from the dependencies and from the list of object files of the command for building who3 in your makefile.
Also, the command for who3.o looks wrong. You are compiling only -c, but you are passing an object file as input (show.o). You should remove show.o from the rule and show.c doesn't belong on the list of dependencies of who3.o either.
Also, the command for show.o looks wrong. You shouldn't be passing header files (show.h) to the compiler; they only need to be referenced as #include in the source files.
Also, you are inconsistent about what your default is actually called. You say it is who3 in the rule (who3: ...) but the command will actually build a task called who (gcc -o who ...).
I'm pulling out my hair trying to figure out why this isn't working on my Minix system.
When I try to compile my C program, I get the following error:
#make
link pm/pm
program.o: In function `do_function':
program.c:(.text+0x1e): undefined reference to `shmget'
program.c:(.text+0x36): undefined reference to `shmat'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
*** Error code 1
Stop.
make: stopped in /usr/src/servers/pm
#
This is my code:
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/stat.h>
/* Some other includes here */
typedef struct {
//struct elemLi *next;
//elem sem;
int ref_count;
} elemLi;
int do_function(void){
int segment_id; //Shared Memory ID
struct elemLi* sli; //Shared Memory Pointer
segment_id = shmget(IPC_PRIVATE, sizeof(elemLi),0660 | IPC_CREAT);
sli = (struct elemLi *)shmat(segment_id,NULL,0);
return -1;
}
As you can see, I've included the proper header files for these calls and it's still saying the references are undefined. I was able to successfully use this in another program, so I've completely run out of ideas as to why this isn't working in this instance.
Edit: This is within a system call. I assume that doesn't make a difference.
Thanks for the help everyone, it looks like it was a missing library in the linker. I added libc, using -lc, to the Makefile, and now it seems to be compiling fine.