ThomasBarth / ESP32-CAN-Driver

CAN driver project for the Espressif ESP32
284 stars 78 forks source link

undefined reference to CAN_cfg #15

Closed physiii closed 6 years ago

physiii commented 6 years ago

Having trouble compile - undefined reference to CAN_cfg:

$ make CC build/can/CAN.o AR build/can/libcan.a CC build/main/hello_world_main.o AR build/main/libmain.a LD build/hello-world.elf /home/physiii/code/esp-examples/get-started/hello_world/build/can/libcan.a(CAN.o):(.literal.CAN_read_frame+0x0): undefined reference to `CAN_cfg' collect2: error: ld returned 1 exit status /home/physiii/esp/esp-idf/make/project.mk:388: recipe for target '/home/physiii/code/esp-examples/get-started/hello_world/build/hello-world.elf' failed

Thomas' blog post has this in CAN_config.h

CAN_device_t CAN_cfg = {
   .speed=CAN_SPEED_500KBPS,
   .tx_pin_id = GPIO_NUM_5,
   .rx_pin_id = GPIO_NUM_4,
   .rx_queue=NULL,
};

Adding that to CAN_config.h in this repo gives:

/home/physiii/code/esp-examples/get-started/hello_world/build/can/libcan.a(CAN.o):(.data.CAN_cfg+0x0): multiple definition of `CAN_cfg' /home/physiii/code/esp-examples/get-started/hello_world/build/main/libmain.a(hello_world_main.o):(.data.CAN_cfg+0x0): first defined here

Idea's on how to compile?

dpharris commented 6 years ago

fgrep -nr CAN_cfg *

On Jan 29, 2018 10:08, "physiii" notifications@github.com wrote:

Having trouble compile - undefined reference to CAN_cfg:

$ make CC build/can/CAN.o AR build/can/libcan.a CC build/main/hello_worldmain.o AR build/main/libmain.a LD build/hello-world.elf /home/physiii/code/esp-examples/get-started/hello world/build/can/libcan.a(CAN.o):(.literal.CAN_read_frame+0x0): undefined reference to `CAN_cfg' collect2: error: ld returned 1 exit status /home/physiii/esp/esp-idf/make/project.mk:388: recipe for target '/home/physiii/code/esp-examples/get-started/hello_world/build/hello-world.elf' failed

Thomas' blog post http://www.barth-dev.de/can-driver-esp32/ has this in CAN_config.h https://github.com/ThomasBarth/ESP32-CAN-Driver/blob/master/components/can/include/CAN_config.h

CAN_device_t CAN_cfg = { .speed=CAN_SPEED_500KBPS, .tx_pin_id = GPIO_NUM_5, .rx_pin_id = GPIO_NUM_4, .rx_queue=NULL, };

Adding that to CAN_config.h in this repo gives:

/home/physiii/code/esp-examples/get-started/hello_ world/build/can/libcan.a(CAN.o):(.data.CAN_cfg+0x0): multiple definition of `CANcfg' /home/physiii/code/esp-examples/get-started/hello world/build/main/libmain.a(hello_world_main.o):(.data.CAN_cfg+0x0): first defined here

Idea's on how to compile?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ThomasBarth/ESP32-CAN-Driver/issues/15, or mute the thread https://github.com/notifications/unsubscribe-auth/AAg4SliRi3BH5SonQGi4mQ1pnkQOm00dks5tPgkYgaJpZM4RxHKW .

physiii commented 6 years ago

$ fgrep -nr CAN_cfg * Binary file build/can/CAN.o matches Binary file build/can/libcan.a matches

adding

CAN_device_t CAN_cfg = {
   .speed=CAN_SPEED_500KBPS,
   .tx_pin_id = GPIO_NUM_5,
   .rx_pin_id = GPIO_NUM_4,
   .rx_queue=NULL,
};

to CAN_config.h gives

$ fgrep -nr CAN_cfg * Binary file build/can/CAN.o matches Binary file build/can/libcan.a matches build/hello-world.map:14661: .data.CAN_cfg 0x0000000000000000 0x10 /home/physiii/code/esp-examples/get-started/hello_world/build/can/libcan.a(CAN.o) build/hello-world.map:19195: .data.CAN_cfg 0x000000003ffb1090 0x10 /home/physiii/code/esp-examples/get-started/hello_world/build/main/libmain.a(hello_world_main.o) build/hello-world.map:19196: 0x000000003ffb1090 CAN_cfg Binary file build/main/hello_world_main.o matches Binary file build/main/libmain.a matches

dpharris commented 6 years ago

Do the fgrep on the source files ... does the sketch itself define ' CAN_cfg'?

On Mon, Jan 29, 2018 at 10:36 AM, physiii notifications@github.com wrote:

$ fgrep -nr CAN_cfg * Binary file build/can/CAN.o matches Binary file build/can/libcan.a matches

adding

CAN_device_t CAN_cfg = { .speed=CAN_SPEED_500KBPS, .tx_pin_id = GPIO_NUM_5, .rx_pin_id = GPIO_NUM_4, .rx_queue=NULL, };

to CAN_config.h gives

$ fgrep -nr CAN_cfg * Binary file build/can/CAN.o matches Binary file build/can/libcan.a matches build/hello-world.map:14661: .data.CANcfg 0x0000000000000000 0x10 /home/physiii/code/esp-examples/get-started/hello world/build/can/libcan.a(CAN.o) build/hello-world.map:19195: .data.CANcfg 0x000000003ffb1090 0x10 /home/physiii/code/esp-examples/get-started/hello world/build/main/libmain.a(hello_world_main.o) build/hello-world.map:19196: 0x000000003ffb1090 CAN_cfg Binary file build/main/hello_world_main.o matches Binary file build/main/libmain.a matches

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ThomasBarth/ESP32-CAN-Driver/issues/15#issuecomment-361342432, or mute the thread https://github.com/notifications/unsubscribe-auth/AAg4SgWxBsyqp9_ajqiKD7eRhjKEut4yks5tPg_HgaJpZM4RxHKW .

physiii commented 6 years ago

Yes - I must be missing something basic. My hello_world_main.c looks like:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "CAN.h"

void app_main()
{
    printf("Hello world!\n");

    CAN_device_t CAN_cfg = {
     .speed=CAN_SPEED_500KBPS,
     .tx_pin_id = GPIO_NUM_5,
     .rx_pin_id = GPIO_NUM_4,
     .rx_queue=NULL,
    };

    CAN_init();
}

output:

$ make CC build/main/hello_world_main.o /home/physiii/code/can_test/main/./hello_world_main.c: In function 'app_main': /home/physiii/code/can_test/main/./hello_world_main.c:10:18: warning: unused variable 'CAN_cfg' [-Wunused-variable] CAN_device_t CAN_cfg = { ^ AR build/main/libmain.a LD build/hello-world.elf /home/physiii/code/can_test/build/can/libcan.a(CAN.o):(.literal.CAN_read_frame+0x0): undefined reference to `CAN_cfg' collect2: error: ld returned 1 exit status

dpharris commented 6 years ago

I think you need to make CAN_cfg global:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "CAN.h"
CAN_cfg = {
    CAN_SPEED_500KBPS,
    GPIO_NUM_5,
    GPIO_NUM_4,
   NULL,
};
void app_main()
{
    printf("Hello world!\n");
    CAN_init();
}
dpharris commented 6 years ago

Might have to be:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "CAN.h"

void app_main()
{
   printf("Hello world!\n");
   CAN_cfg = {
      CAN_SPEED_500KBPS,
      GPIO_NUM_5,
      GPIO_NUM_4,
      NULL
   };
   CAN_init();
}
collin80 commented 6 years ago

That's the one. Of course you need the structure name too. I fixed that in the below code sample. The code should compile as long as you define a global variable called CAN_cfg

On Mon, Jan 29, 2018 at 5:12 PM, David Harris notifications@github.com wrote:

I think you need to make CAN_cfg global:

include

include "freertos/FreeRTOS.h"

include "freertos/task.h"

include "CAN.h"

CAN_device_t CAN_cfg = { CAN_SPEED_500KBPS, GPIO_NUM_5, GPIO_NUM_4, NULL, }; void app_main() { printf("Hello world!\n"); CAN_init(); }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ThomasBarth/ESP32-CAN-Driver/issues/15#issuecomment-361404931, or mute the thread https://github.com/notifications/unsubscribe-auth/ACC7ZZxQtf3LHHyg1IExob04K1tOGutTks5tPkJrgaJpZM4RxHKW .

ThomasBarth commented 6 years ago

Don't put the config itself into the header. This will create a global variable in each file including this header.