Linking to Homebrew library within Make

I'm trying to link the libserialport library from within a C program in CLion.  The library is located at /opt/homebrew/Cellar/libserialport/0.1.1/ on my Mac.

I'm getting the following linker error:

[1/2] Linking C executable temp_libserialport_v4
FAILED: temp_libserialport_v4 
: && /Library/Developer/CommandLineTools/usr/bin/cc -g -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/temp_libserialport_v4.dir/main.c.o -o temp_libserialport_v4  /opt/homebrew/Cellar/libserialport/0.1.1/lib/libserialport.a && :
Undefined symbols for architecture arm64:
 "_CFNumberGetValue", referenced from:
     _get_port_details in libserialport.a[3](macosx.o)
     _get_port_details in libserialport.a[3](macosx.o)
     _get_port_details in libserialport.a[3](macosx.o)
     _get_port_details in libserialport.a[3](macosx.o)

in response to the following code:

#include <stdio.h>
#include <libserialport.h>

int main(void)
{
    printf("Hello, World!\n");

    /* A pointer to a null-terminated array of pointers to
     * struct sp_port, which will contain the ports found.*/
    struct sp_port **port_list;

    printf("Getting port list.\n");

    /* Call sp_list_ports() to get the ports. The port_list
     * pointer will be updated to refer to the array created. */
    enum sp_return result = sp_list_ports(&port_list);

    return 0;
}

I'm not sure how to resolve the linker issue.  Is there something that I should be changing in my CMake file?  This is what I have so far:

cmake_minimum_required(VERSION 3.28)
project(temp_libserialport_v4 C)

set(CMAKE_C_STANDARD 23)

# try to tie in libserial port.  It's in
# /opt/homebrew/Cellar/libserialport/0.1.1
#       ... libserialport.a in /lib and
#       ... libserialport.h  in /include
# brew info libserialport

# Set the path to the libserialport Homebrew library
set(HOMEBREW_LIB_DIR /opt/homebrew/Cellar/libserialport/0.1.1)

# Include the library's headers
include_directories(${HOMEBREW_LIB_DIR}/include)

# Link against the static library
add_executable(temp_libserialport_v4 main.c)
target_link_libraries(temp_libserialport_v4 PRIVATE ${HOMEBREW_LIB_DIR}/lib/libserialport.a)

 

thanks!

James

 

0

I've posted the solution on my blog here:

https://www.yorku.ca/professor/drsmith/2024/07/24/clion-libserialport/

It boils down to how I was missing the following (linking to the serial port library):

target_link_libraries(${PROJECT_NAME}  serialport)

-James

0

请先登录再写评论。