0

I am currently working on a project using netcdf-c library. I have installed it using Homebrew. I have written a small script to write a .NC file. I am using a Mac

When I compile it in CLion using make main the following gets outputted:

make main
cc     main.c   -o main
Undefined symbols for architecture x86_64:
"_nc_close", referenced from:
  _main in main-f43ab9.o
"_nc_get_var_int", referenced from:
  _main in main-f43ab9.o
"_nc_inq_varid", referenced from:
  _main in main-f43ab9.o
"_nc_open", referenced from:
  _main in main-f43ab9.o
"_nc_strerror", referenced from:
  _main in main-f43ab9.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1

This is the content of my Cmakelists.txt:

cmake_minimum_required(VERSION 3.6)
project(hello)

INCLUDE_DIRECTORIES(/usr/local/include)
LINK_DIRECTORIES(/usr/local/lib)

set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} /usr/local/lib)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} cc -stdlib=lnetcdf -Wall -Werror main.c")

INCLUDE_DIRECTORIES(/usr/local/include)
LINK_DIRECTORIES(/usr/local/lib)

set(SOURCE_FILES main.c)
add_executable(hello ${SOURCE_FILES})

Despite this I am able to execute my code by using the terminal and typing:

cc -lnetcdf main.c

Could someone tell me where the problem is?

Thanks

Bart
  • 9,825
  • 5
  • 47
  • 73
Ahmed Ali
  • 75
  • 2
  • 11
  • There seem to be multiple problems with your `CMakeLists.txt`: `-stdlib` is used to set the C++ standard library (see e.g. http://stackoverflow.com/a/19774902/3581217), the `CMAKE_C_FLAGS` are used to set the compiler flags, not the compiler itself (`cc`), source files (`main.c`), et cetera. I'm sorry to put it like this, but I would start by reading some `CMake` tutorials. – Bart Nov 09 '16 at 18:29
  • Also, in the future add `cmake` as a tag; that's probably the most relevant tag for your question – Bart Nov 09 '16 at 18:31

0 Answers0