CMake Unable to Link PostgreSQL Libraries

I need help with PostgreSQL extensions in C

I have a small test extension to see if something is working. It seems that cmake finds Libraries, but can only link GDAL to my project. When creating, I get this error message:

/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/Max/Library/Caches/clion10/cmake/generated/6306a90e/6306a90e/Debug --target Project -- -j 2 Scanning dependencies of target Project [100%] Building C object CMakeFiles/Project.dir/main.co Linking C executable Project Undefined symbols for architecture x86_64: "_Float4GetDatum", referenced from: _main in main.co ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[3]: *** [Project] Error 1 make[2]: *** [CMakeFiles/Project.dir/all] Error 2 make[1]: *** [CMakeFiles/Project.dir/rule] Error 2 make: *** [Project] Error 2 

main.c

 #include <postgres.h> #include <ogr_api.h> #include <stdio.h> int main() { // GDAL Function compiles without errors OGRGeometryH g = OGR_G_CreateGeometryFromJson("POINT(0 0)"); float4 f = 4.6; // Postgres Function doesnt work Float4GetDatum(f); OGR_G_DestroyGeometry(g); printf("%f", f); printf("Hello, World!"); return 0; } 

CMakeLists.txt

 cmake_minimum_required(VERSION 3.1) project(Project) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror") set(PostgreSQL_INCLUDE_DIR /usr/local/pgsql-9.1/include/server) set(PostgreSQL_LIBRARY_DIR /usr/local/pgsql-9.1/lib) find_package(PostgreSQL REQUIRED) find_package(GDAL REQUIRED) include_directories(${PostgreSQL_INCLUDE_DIR} ${GDAL_INCLUDE_DIR} ) set(SOURCE_FILES main.c) add_executable(Project ${SOURCE_FILES}) target_link_libraries(Project ${PostgreSQL_LIBRARIES} ${GDAL_LIBRARY} ) 

OS: Mac OSX 10.10.3 Postgres: 9.1.8

I am using the CLION IDE. Btw the database itself works fine.

EDIT: Detailed Description Output

 /Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/Max/Library/Caches/clion10/cmake/generated/6306a90e/6306a90e/Debug --target Project -- -j 2 /Applications/CLion.app/Contents/bin/cmake/bin/cmake -H/Users/Max/Development/CLionProjects/Project -B/Users/Max/Library/Caches/clion10/cmake/generated/6306a90e/6306a90e/Debug --check-build-system CMakeFiles/Makefile.cmake 0 /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 Project /Applications/CLion.app/Contents/bin/cmake/bin/cmake -H/Users/Max/Development/CLionProjects/Project -B/Users/Max/Library/Caches/clion10/cmake/generated/6306a90e/6306a90e/Debug --check-build-system CMakeFiles/Makefile.cmake 0 /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_progress_start /Users/Max/Library/Caches/clion10/cmake/generated/6306a90e/6306a90e/Debug/CMakeFiles 1 /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 CMakeFiles/Project.dir/all /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Project.dir/build.make CMakeFiles/Project.dir/depend cd /Users/Max/Library/Caches/clion10/cmake/generated/6306a90e/6306a90e/Debug && /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_depends "Unix Makefiles" /Users/Max/Development/CLionProjects/Project /Users/Max/Development/CLionProjects/Project /Users/Max/Library/Caches/clion10/cmake/generated/6306a90e/6306a90e/Debug /Users/Max/Library/Caches/clion10/cmake/generated/6306a90e/6306a90e/Debug /Users/Max/Library/Caches/clion10/cmake/generated/6306a90e/6306a90e/Debug/CMakeFiles/Project.dir/DependInfo.cmake --color= Scanning dependencies of target Project /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Project.dir/build.make CMakeFiles/Project.dir/build /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_progress_report /Users/Max/Library/Caches/clion10/cmake/generated/6306a90e/6306a90e/Debug/CMakeFiles 1 [100%] Building C object CMakeFiles/Project.dir/main.co /usr/bin/gcc -Wall -Werror -g -I/usr/local/pgsql-9.1/include/server -I/Library/Frameworks/GDAL.framework/Headers -o CMakeFiles/Project.dir/main.co -c /Users/Max/Development/CLionProjects/Project/main.c Linking C executable Project /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/Project.dir/link.txt --verbose=1 /usr/bin/gcc -Wall -Werror -g -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/Project.dir/main.co -o Project -lpq -framework gdal Undefined symbols for architecture x86_64: "_Float4GetDatum", referenced from: _main in main.co ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[3]: *** [Project] Error 1 make[2]: *** [CMakeFiles/Project.dir/all] Error 2 make[1]: *** [CMakeFiles/Project.dir/rule] Error 2 make: *** [Project] Error 2 

EDIT 2:

had "PostgreSQL_TYPE_INCLUDE_DIR = not found" in my Cmake cache so I added set(PostgreSQL_TYPE_INCLUDE_DIR /usr/local/pgsql-9.1/include/server/catalog) to my CMakeList

CMakeCache

 //Path to a program. GDAL_CONFIG:FILEPATH=/usr/local/bin/gdal-config //Path to a file. GDAL_INCLUDE_DIR:PATH=/Library/Frameworks/GDAL.framework/Headers //Path to a library. GDAL_LIBRARY:FILEPATH=/Library/Frameworks/gdal.framework //Path to a library. PostgreSQL_LIBRARY:FILEPATH=/usr/lib/libpq.dylib //Internal cache //Details about finding GDAL FIND_PACKAGE_MESSAGE_DETAILS_GDAL:INTERNAL=[/Library/Frameworks/gdal.framework][/Library/Frameworks/GDAL.framework/Headers][v()] //Details about finding PostgreSQL FIND_PACKAGE_MESSAGE_DETAILS_PostgreSQL:INTERNAL=[/usr/lib/libpq.dylib][/usr/local/pgsql-9.1/include/server][/usr/local/pgsql-9.1/include/server/catalog][v9.1.8()] //ADVANCED property for variable: PostgreSQL_LIBRARY PostgreSQL_LIBRARY-ADVANCED:INTERNAL=1 
+6
source share

Source: https://habr.com/ru/post/985464/


All Articles